sfc Posted September 1, 2010 Share Posted September 1, 2010 Question 1) Is the only and proper way to call a parent function "parent::function()"? Are there other/better ways from within a child function? Question 2) What are the deciding factors for when to make a function or attribute static? How do you make that decision? Assuming 5.3... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/212311-two-questions-1-calling-parent-function-2-static-function/ Share on other sites More sharing options...
JonnoTheDev Posted September 2, 2010 Share Posted September 2, 2010 Question 1) Is the only and proper way to call a parent function "parent::function()"? Are there other/better ways from within a child function? No. See below. <?php class foo { public function __construct() { } protected function dostuff() { print "Hello World"; } } class bar extends foo { public function __construct() { parent::__construct(); $this->dostuff(); } } $x = new bar(); ?> Question 2) What are the deciding factors for when to make a function or attribute static? How do you make that decision? When you want it to belong to the whole class and not a specific instance of a class (object). i.e <?php class foo { protected static $counter = 0; public $num; public function __construct() { self::$counter++; $this->num = self::$counter; } } // same class - 2 individual objects $x = new foo(); print $x->num."<br />"; // prints 1 $y = new foo(); print $y->num."<br />"; // prints 2 ?> This essentially means that the static property or method means something to the class but not to the individual object. i.e <?php class foo { public static function convertLbToKg($pounds) { return $pounds * 0.45359; } } class bar { public $kg; public function __construct($pounds) { $this->kg = foo::convertLbToKg($pounds); } } // I can call the static method from outside of the class print foo::convertLbToKg(120)."kg<br />"; // Or it can be called from within another class when creating an object $x = new bar(150); print $x->kg."kg<br />"; ?> I would recommend you invest in the following: http://friendsofed.com/book.html?isbn=9781430210115 Quote Link to comment https://forums.phpfreaks.com/topic/212311-two-questions-1-calling-parent-function-2-static-function/#findComment-1106403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.