4

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)

choz
  • 17,242
  • 4
  • 53
  • 73
  • Take a look [here](http://stackoverflow.com/questions/3754786/calling-non-static-method-with), basically, PHP will warn you (if you have error reporting enabled). You won't have access to any instance variables if you call a non-static function statically however. So if you try to use any variables that only apply to an object (i.e instance variables), you'll get a fatal error. – Dave Chen Jun 29 '15 at 14:34
  • Thanks for your reply dave.. The thread that u refer is pretty useful and thanks to that Im more confused on how I should implement this class of mine. Is there any downside for calling non static function with ::? – choz Jun 30 '15 at 01:34
  • As far as I can see, there's only a warning generated by PHP when you call an instance method statically. If you call a static method objectively there's no error. So you can always get away with `->`. – Dave Chen Jun 30 '15 at 04:02

1 Answers1

0

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.

User366
  • 765
  • 4
  • 9