191

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

$this->staticMethod();

or

$this::staticMethod();
tereško
  • 58,060
  • 25
  • 98
  • 150
ajsie
  • 77,632
  • 106
  • 276
  • 381
  • 13
    You might be interested in this (`self` vs. `$this`): http://stackoverflow.com/questions/151969/php-self-vs-this – Felix Kling Feb 04 '10 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. – joejoeson Feb 05 '10 at 00:32
  • you can delete the $this now please it doesn't work if only using static methods and no instance exists. – malhal Nov 10 '13 at 02: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* – Otheus Sep 06 '21 at 09:47

5 Answers5

386
self::staticMethod();

More information about the Static keyword.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • 1
    ...but why? $this->staticMethod() works too. Can you explain why self::staticMethod() more correct (if it is)? – Ian Dunn Mar 31 '12 at 04:22
  • 34
    @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`. – jeroen Mar 31 '12 at 13:00
  • 1
    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::` – blamb Jan 22 '18 at 19:56
47

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.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • `self::bar()` seems misleading - is that now deprecated? (using `self::` to call an instance method rather than a static method). – ToolmakerSteve Aug 17 '16 at 16:49
  • @ToolmakerSteve in what way would you say that it's misleading? – Ja͢ck Aug 17 '16 at 16:57
  • 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`. – ToolmakerSteve Apr 08 '19 at 13:20
19

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(); 
Joundill
  • 6,828
  • 12
  • 36
  • 50
  • 2
    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. – ToolmakerSteve Aug 17 '16 at 16:54
  • 1
    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`. – iyrin Apr 04 '17 at 13:56
2

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();
    }
}
Nishad Up
  • 3,457
  • 1
  • 28
  • 32
  • 1
    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. – ToolmakerSteve Apr 08 '19 at 14:49
0

call a static method inside a class

className::staticFunctionName

example

ClassName::staticMethod();

Theakash
  • 195
  • 4