Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Static Method Using Private Class Functions/Variables

If I write a public static method in a class ie...

public static function get_info($type){
        switch($type){
            case'title':
                self::get_title(); 
                break;
        }
    }

I have to write my get_title() function as public...

public static function get_title(){
        return 'Title';
    }

Otherwise I get the error:

Call to private method Page::get_title()

Which makes me feel as though the function get_info() is essentially redundant. I'd like to be able to make a call from a static method to a private method inside my class for validation purposes. Is this impossible?

PHP > 5.0 btw.

!####### EDIT SOLUTION (BUT NOT ANSWER TO QUESTION) #########!

In case you are curious, my workaround was to instantiate my static function's class inside the static function.

So, the class name was Page I would do this...

public static function get_info($type){
            $page = new Page();
            switch($type){
                case'title':
                    $page->get_title(); 
                    break;
            }
        }
  public function get_title(){
            return 'Title';
        }
like image 593
Howard Zoopaloopa Avatar asked Jul 28 '10 21:07

Howard Zoopaloopa


2 Answers

This is actually OK, there is nothing impossible here as far as I can see. Your static get_title() method can be private - or have I missed something? If both your static methods, get_info() and get_title(), are in the same class (whether it's static or not) then your get_title() method can be private and your code still works without error. get_info() calls get_title() inside the class - statically. get_title() does not need to be public in your example, unless it needs to be accessible from outside that static class.

Access (public, protected and private) applies to static classes (where all methods are static) as well as class instances.

EDIT: You don't need to resort to instantiating the class in order to implement the private access...

// Enable full error reporting to make sure all is OK
error_reporting(E_ALL | E_STRICT);

class MyStaticClass {

 public static function get_info($type){
  switch($type){
   case 'title':
    return self::get_title(); 
    break;
   }
 }


 private static function get_title() {
  return 'Title';
 }
}

// OK - get_info() calls the private method get_title() inside the static class
echo MyStaticClass::get_info('title');

// ERROR - get_title() is private so cannot be called from outside the class
echo MyStaticClass::get_title();
like image 176
MrWhite Avatar answered Oct 22 '22 14:10

MrWhite


yes, it's impossible - a non-static method needs an object to read data from, while the point of a static one is that it has no such object attached. you can think of each non-static method of being passed an implicit argument, the object. you simply don't have a value to pass this value on to the method if you're calling from a static function.

update you can have private static function - I'm not sure if your question might involve a slight misunderstanding of private and static as mutually exclusive concepts

like image 32
Nicolas78 Avatar answered Oct 22 '22 15:10

Nicolas78