Search code examples
phpapacheoopmodel-view-controllersymfony1

Why can I call non-static function without declaring the class object?


I am using Symfony 1.0, and I have this MyClassInc.class.php in my project/lib folder

class MyClassInc {
  public function validateFunction ($params) {
    // my codes
  }
  static function testFunction ($params){
    // my codes
  }
}

Then, my action actions.class.php in my project/apps/myapps/modules/actions.

class inventoryCycleCountActions extends sfActions
{
  public function validateOutstandingTransaction () {
    $res0 = MyClassInc :: validateFunction($param); // It works
    $res1 = MyClassInc :: testFunction($param); // It works
    $myClass = new MyClassInc();
    $res2 = $myClass->validateFunction($param); // still works
    $res3 = $myClass->testFunction($param); // still works, da hell?
  }
}

I tried to clear my cache folder to do re-test, but it seems that all of those work just fine.

Question: So.. WHY? and which one should I use? Does it have any effect with performance or anything?

Update 1:

class MyClassInc {
  public function isProductValidated ($product){
    return true;
  }
  public function validateFunction ($params) {
    // IF, I call by using "$res0".. Throws error
    //
    $this->isProductInLoadPlans($product);
  }
}

If I call validateFunction via $res0, it will throw this error:

sfException: Call to undefined method inventoryCycleCountActions::isProductValidated.

And, if I call it via $res2, it works just fine.

Since, I am currently using $res0 and so I have to call that method like this instead.

MyClassInc :: isProductValidated ($product)


Solution

  • The only real difference between :: and -> is how $this is handled. With :: the function will have $this as it was defined in the caller's scope:

    class A {
    
      public function foo() {
        A::bar();
        A::foobar();
      }
    
      static private function bar() {
         // $this here is the instance of A
      }
    
      static public function foobar() {
        // Here you can have anything in $this (including NULL)
      }
    }
    
    $a = new A;
    $a->foo();
    $a->foobar(); // $this == $a but bad style
    A::foobar(); // $this == NULL
    

    When you want to use an instance method, you should use -> because that would resolve instance methods properly (including inheritance). :: will always call the method of the specified class.

    I believe there is effort made now to enforce calling static methods only as statically and dynamic methods only dynamically to avoid the confusion.