Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope resolution operator and class method

Well, I don't get why this code works

class MyObject  {
    public function myBaseMethod()
    {
        echo 'I\'m declared in' . __CLASS__;
    }

}

$instance = new MyObject();
$instance->myBaseMethod();
MyObject::myBaseMethod();

 // Output
I'm declared inMyObject
I'm declared inMyObject

I can invoke myBaseMethod() by creating new instance of a MyObject class or as static method. But myBaseMethod is not declared as static.

I thought that I can use :: only for static members/methods.

Any explanations pls

like image 857
dofenco Avatar asked Dec 05 '25 06:12

dofenco


1 Answers

Yeah it is possible. I would like to know why. But note that PHP will throw an E_STRICT error which is definitely not acceptable for new code while it might happen in legacy code. You should not call methods static which were not declared as static. Try to fix legacy code.

like image 118
hek2mgl Avatar answered Dec 08 '25 01:12

hek2mgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!