7

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';
        }
12
  • I was under the impression that this was possible and indeed I can (after some checking) confirm to have done this on several occasions. Are you sure get_info() and get_title() are in the same class? It won’t work if get_title() resides in a superclass… Commented Jul 28, 2010 at 22:01
  • what happens then, when you refer to $this in the called non-static function? Commented Jul 28, 2010 at 22:05
  • @raphael... I tried, man. The error I got above was a result of calling the get_title() method "private". Commented Jul 28, 2010 at 22:09
  • 1
    @nicolas78 - True, you can't refer to $this as there is no object instance, and you can't use $this to call a static method anyway. But you can use self:: to refer to the static class that the method is in, in order to call other methods (private, protected or public) in that static class. Commented Jul 28, 2010 at 22:10
  • @Jascha - I also tried your code (both methods in the same class) and it works OK get_title() with private access. Commented Jul 28, 2010 at 22:11

2 Answers 2

11

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();
Sign up to request clarification or add additional context in comments.

Comments

2

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

5 Comments

So static methods inside of a class or really just utility functions in a "name space" wrapper?
Basically, yes. They can also access static data (although this is another word for global variables, so the usual warnings apply).
What's impossible here? I'm not sure that this is answering the question?
well if the question is 'is this impossible', 'yes it's impossible' sure seems like a valid answer at least syntactically :)
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.