202

how do i call a static method from another method inside the same class?

$this->staticMethod();

or

$this::staticMethod();
4
  • 13
    You might be interested in this (self vs. $this): stackoverflow.com/questions/151969/php-self-vs-this Commented Feb 4, 2010 at 23:35
  • 1
    Just an FYI, your first example is an instance variable calling a static method which is not possible because a static method is part of the class and is not accessible through an instance variable. Commented Feb 5, 2010 at 0:32
  • you can delete the $this now please it doesn't work if only using static methods and no instance exists. Commented Nov 10, 2013 at 2:16
  • Unbelievable how hard it is to find this question and answer. It's because PHP doesn't refer to them as 'class method' or 'class function'. eyeroll Commented Sep 6, 2021 at 9:47

5 Answers 5

407
self::staticMethod();

More information about the Static keyword.

Sign up to request clarification or add additional context in comments.

3 Comments

...but why? $this->staticMethod() works too. Can you explain why self::staticMethod() more correct (if it is)?
@Ian Dunn Put simply, $this only exists if an object has been instantiated and you can only use $this->method from within an existing object. If you have no object but just call a static method and in that method you want to call another static method in the same class, you have to use self::. So to avoid potential errors (and strict warnings) it is better to use self.
Thanks! In laravel, i found that i was accidentally calling the static method across an extended controller using $this, but the problem didnt surface until code was pushed to stage. no errors came back, the value was just 0. be careful with this, use self::
48

Let's assume this is your class:

class Test
{
    private $baz = 1;

    public function foo() { ... }

    public function bar() 
    {
        printf("baz = %d\n", $this->baz);
    }

    public static function staticMethod() { echo "static method\n"; }
}

From within the foo() method, let's look at the different options:

$this->staticMethod();

So that calls staticMethod() as an instance method, right? It does not. This is because the method is declared as public static the interpreter will call it as a static method, so it will work as expected. It could be argued that doing so makes it less obvious from the code that a static method call is taking place.

$this::staticMethod();

Since PHP 5.3 you can use $var::method() to mean <class-of-$var>::; this is quite convenient, though the above use-case is still quite unconventional. So that brings us to the most common way of calling a static method:

self::staticMethod();

Now, before you start thinking that the :: is the static call operator, let me give you another example:

self::bar();

This will print baz = 1, which means that $this->bar() and self::bar() do exactly the same thing; that's because :: is just a scope resolution operator. It's there to make parent::, self:: and static:: work and give you access to static variables; how a method is called depends on its signature and how the caller was called.

To see all of this in action, see this 3v4l.org output.

3 Comments

self::bar() seems misleading - is that now deprecated? (using self:: to call an instance method rather than a static method).
@ToolmakerSteve in what way would you say that it's misleading?
Logically speaking, there is no self when invoking a static method. By definition: the static method is callable from anywhere, and does not receive a "self" parameter. Nevertheless, I see the convenience of that php syntax, so that you don't have to write MyClassName::. I'm used to statically-typed languages, where the compiler has to be given all variables available in the current scope, so (the equivalent of) self:: can be omitted. So one only said self instanceMethod; no reason to say self staticMethod.
21

This is a very late response, but adds some detail on the previous answers

When it comes to calling static methods in PHP from another static method on the same class, it is important to differentiate between self and the class name.

Take for instance this code:

class static_test_class {
    public static function test() {
        echo "Original class\n";
    }

    public static function run($use_self) {
        if($use_self) {
            self::test();
        } else {
            $class = get_called_class();
            $class::test(); 
        }
    }
}

class extended_static_test_class extends static_test_class {
    public static function test() {
        echo "Extended class\n";
    }
}

extended_static_test_class::run(true);
extended_static_test_class::run(false);

The output of this code is:

Original class

Extended class

This is because self refers to the class the code is in, rather than the class of the code it is being called from.

If you want to use a method defined on a class which inherits the original class, you need to use something like:

$class = get_called_class();
$class::function_name(); 

2 Comments

I found this informative. A small nit, I would not say that the other answers are "misleading". More accurate to say that they are "incomplete"; they don't address the (unasked) question of what self:: does in the (rare) case where a static method A calls another static method B, and B has been overridden in a subclass. IMHO, it is less confusing to restrict method overriding to "instance" methods; use that ability sparingly at the static level. Put it another way, readers of your code expect method overriding of instance methods (that is the essence of OO coding), but not of static ones.
Very helpful and it makes sense that an extension of the class is not the original class. Therefore it stands to reason that self would not be used in that case. You've declare a separate class as an extension to the first class. Using self within the extended class would refer to the extended class. This does not contradict the other answers, but it certainly helps demonstrate the scope of self.
3

In the later PHP version self::staticMethod(); also will not work. It will throw the strict standard error.

In this case, we can create object of same class and call by object

here is the example

class Foo {

    public function fun1() {
        echo 'non-static';   
    }

    public static function fun2() {
        echo (new self)->fun1();
    }
}

1 Comment

You could do that, though if fun1 isn't making use of self, it isn't logical to make it an instance method. The proper way to do this in php is to declare public static function fun1, then call by specifying the class: Foo::fun1. I'm certain that's the intended way to fix that strict standard error.
0

call a static method inside a class

className::staticFunctionName

example

ClassName::staticMethod();

Comments

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.