views:

139

answers:

2

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';
        }
A: 

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

Nicolas78
So static methods inside of a class or really just utility functions in a "name space" wrapper?
Jascha
Basically, yes. They can also access static data (although this is another word for global variables, so the usual warnings apply).
JW
What's _impossible_ here? I'm not sure that this is answering the question?
w3d
well if the question is 'is this impossible', 'yes it's impossible' sure seems like a valid answer at least syntactically :)
Nicolas78
of course syntax isn't everything and so I was trying to argue that even if it should turn out possible syntactically for some odd reason, there is no semantics to what such a call could possibly mean
Nicolas78
A: 

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();
w3d