Jump to content
Larry Ullman's Book Forums

Add Explanation For Class::Method() Vs Object->Method() ?


olivier
 Share

Recommended Posts

Dear Larry,

 

p13 :

 

 

 

 

$obj = new SomeClass();

$obj->doThis();

 

 

p 53:

$app = Yii::createWebApplication($config);

$app->run();

 

I Searched on the Internet and found the explanation about Static methods, but maybe you could add a short explanation p13 about this ?

 

I found a clear answer here

http://stackoverflow.com/questions/3961456/difference-double-colon-and-arrow-operators-in-php

 

my 2 cents...

 

Thanks for reading.

Olivier.

Link to comment
Share on other sites

Static methods can be called directly from a class without having to make an instance of it with the new keyword. So its similar to calling a function but instead you need to call it via the class name with scope resolution operator "::" followed by the method name.

 

For example

 

Class Edward {

 

static public function GetName() {

echo "My name is Edward";

}

 

}

 

Edward::GetName(); // Echo's 'My name is Edward';

Link to comment
Share on other sites

  • 3 weeks later...

Larry - as a relatively new Yii user I find one of the most difficult part of the game finding how given class can be addressed. I mean - it is often difficult to guess how to use given class' method in the application scope as it is often unclear is given class already instancialized or not, should given method be called statically or rather through an object, etc.

 

You may see very often people asking questions like "how to call a method of one controller from another one" etc. This is all about the issue I tried to describe above. I do not know is it possible to give some general advices how to read documentation to resolve such issues but perhaps you know better and can share...

Link to comment
Share on other sites

The real problem is documentation, where way of invoking is never mentioned and only through tiresome and tedious code reverse-engineerng one may learn for instance that controller's actions are actually created using reflection - so no wonder it's impossible to create new action dynamically (in runtime) using any "regular" measure.

 

Though Yii documentaion is extraordinary complete as for open-source project standards - it lacks information about class invoking and delegates the task to personal experience of Yii users. Unfortunately, this style very often renders community input difficult to read by a novice users as though one my learn on various fora how to "extend" given class - he or she still remains in state of perfect confusion: "but how the hell shall I invoke this class to apply one of its great methods in my particular case???".

 

Best regards,

Ziggi

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...