Php call non static method

Why can't I use a method non-static with the syntax of the methods static[class::method] ? Is it some kind of configuration issue?

class Teste {

    public function fun1[] {
        echo 'fun1';
    }
    public static function fun2[] {
        echo "static fun2" ;
    }
}

Teste::fun1[]; // why?
Teste::fun2[]; //ok - is a static method

Machavity

30.1k26 gold badges87 silver badges98 bronze badges

asked Sep 20, 2010 at 19:24

2

PHP is very loose with static vs. non-static methods. One thing I don't see noted here is that if you call a non-static method, ns statically from within a non-static method of class C, $this inside ns will refer to your instance of C.

class A 
{
    public function test[]
    {
        echo $this->name;
    }
}

class C 
{
     public function q[]
     {
         $this->name = 'hello';
         A::test[];
     }
}

$c = new C;
$c->q[];// prints hello

This is actually an error of some kind if you have strict error reporting on, but not otherwise.

answered Sep 20, 2010 at 20:31

davidtbernaldavidtbernal

13.2k9 gold badges44 silver badges59 bronze badges

2

This is a known "quirk" of PHP. It's by design to prevent back-propagation for figuring out if some time ago we actually instantiated an object or not [remember, PHP is interpreted, not compiled]. However, accessing any non-static member the via scope resolution operator if the object is not instantiated will issue a fatal error.

Courtesy of PHP.net:

class User {
    const GIVEN = 1;  // class constants can't be labeled static nor assigned visibility
    public $a=2;
    public static $b=3;

    public function me[]{
        echo "print me";
    }
     public static function you[] {
        echo "print you";
    }
}

class myUser extends User {
}

// Are properties and methods instantiated to an object of a class, & are they accessible?
//$object1= new User[];        // uncomment this line with each of the following lines individually
//echo $object1->GIVEN . "
"; // yields nothing //echo $object1->GIVE . "
"; // deliberately misnamed, still yields nothing //echo $object1->User::GIVEN . "
"; // yields nothing //echo $object1->a . "
"; // yields 2 //echo $object1->b . "
"; // yields nothing //echo $object1->me[] . "
"; // yields print me //echo $object1->you[] . "
"; // yields print you // Are properties and methods instantiated to an object of a child class, & are accessible? //$object2= new myUser[]; // uncomment this line with each of the following lines individually //echo $object2->GIVEN . "
"; // yields nothing //echo $object2->a . "
"; // yields 2 //echo $object2->b . "
"; // yields nothing //echo $object2->me[] . "
"; // yields print me //echo $object2->you[] . "
"; // yields print you // Are the properties and methods accessible directly in the class? //echo User::GIVEN . "
"; // yields 1 //echo User::$a . "
"; // yields fatal error since it is not static //echo User::$b . "
"; // yields 3 //echo User::me[] . "
"; // yields print me //echo User::you[] . "
"; // yields print you // Are the properties and methods copied to the child class and are they accessible? //echo myUser::GIVEN . "
"; // yields 1 //echo myUser::$a . "
"; // yields fatal error since it is not static //echo myUser::$b . "
"; // yields 3 //echo myUser::me[] . "
"; // yields print me //echo myUser::you[] . "
"; // yields print you ?>

answered Sep 20, 2010 at 19:35

David TitarencoDavid Titarenco

31.9k13 gold badges62 silver badges109 bronze badges

0

This is PHP 4 backwards compatibility. In PHP 4 you could not differ between an object method and the global function written as a static class method. Therefore both did work.

However with the changes in the object model with PHP 5 - //php.net/oop5 - the static keyword has been introduced.

And then since PHP 5.1.3 you get proper strict standard warnings about those like:

Strict Standards: Non-static method Foo::bar[] should not be called statically

And/Or:

Strict Standards: Non-static method Foo::bar[] should not be called statically, assuming $this from incompatible context

which you should have enabled for your development setup. So it's merely backwards compatibility to a time where the language couldn't differ enough so this was "defined" at run-time.

Nowadays you can define it already in the code, however the code will not break if you still call it "wrong".

Some Demo to trigger the error messages and to show the changed behavior over different PHP versions: //3v4l.org/8WRQH

answered Sep 9, 2013 at 9:09

hakrehakre

187k48 gold badges419 silver badges802 bronze badges

PHP 4 did not have a static keyword [in function declaration context] but still allowed methods to be called statically with ::. This continued in PHP 5 for backwards compatibility purposes.

answered Sep 20, 2010 at 19:42

webbiedavewebbiedave

47.7k8 gold badges87 silver badges100 bronze badges

0

You can do this, but your code will error if you use $this in the function called fun1[]

answered Sep 20, 2010 at 19:26

Jake NJake N

10.3k10 gold badges64 silver badges108 bronze badges

Warning In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future.

Link

answered Oct 27, 2017 at 14:26

Malus JanMalus Jan

1,5292 gold badges18 silver badges25 bronze badges

4

Starting on PHP8, this no longer works.

The ability to call non-static methods statically was finally removed in PHP 8.

The ability to call non-static methods statically has been removed. Thus is_callable[] will fail when checking for a non-static method with a classname [must check with an object instance].

It was originally deprecated on PHP 7.

Static calls to methods that are not declared static are deprecated, and may be removed in the future.

answered Dec 14, 2021 at 10:47

yiviyivi

37.3k18 gold badges92 silver badges119 bronze badges

2

In most languages you will need to have an instance of the class in order to perform instance methods. It appears that PHP will create a temporary instance when you call an instance method with the scope resolution operator.

answered Sep 20, 2010 at 19:29

Jacob RelkinJacob Relkin

158k32 gold badges341 silver badges318 bronze badges

Not sure why PHP allows this, but you do not want to get into the habit of doing it. Your example only works because it does not try to access non-static properties of the class.

Something as simple as:

Chủ Đề