question about User::mode()->somefunction() call

Hi All

Currently I’m writing a class which extends from CActiveRecordBehavior:




class Test extends CActiveRecordBehavior

{

       public function xyz(){ }

}



I noticed that there are two ways you can call the functions in this class (assume that User implements this behavior)

  1. from an instance object

$user = new User()

$user->xyz() ;






User::model()->xyz() ;



Both work, but what if this xyz function requires an initialized object with filled attributes, meaning the second call will fail.

First I thought the second call is equivalent to the first (a new instance), but it isn’t.

Can someone explain the differences, and how to forbid the second call ?

cheers

Luca

Use second call only for “static” methods (e.g. findAll()), because in this case a new instance of the User class is returned. We can’t simply call User::findAll() because late static bindings are supported only since PHP 5.3.0.

A little php tip:


class SomeClass {

   public static function SomeStaticMethod() {

      $obj = new SomeClass;

      return $obj;

   }

   public static function DoSomething() {

      // Do something

      return $obj;

   }

}


SomeClass::SomeStaticMethod()->DoSomething();

thnx a lot for both replies!!