note 91013 added to language.oop5.decon

From: Date: Thu, 21 May 2009 01:43:08 +0000
Subject: note 91013 added to language.oop5.decon
Groups: php.notes 
Request: Send a blank email to php-notes+get-155814@lists.php.net to get a copy of this message
I just thought I'd share some valuable information that might help novice programmers/scripters in __constructor() functions. I've been using PHP for quite a long time, as well as programming and scripting in general. Even with my quite seasoned skillset, I still make obvious, painstaking mistakes. That said, I'd like to point out one major (albeit obvious) common mistake that programmers and scripters (such as myself) make. When defining the constructor for an object and using it to populate variables for use throughout the lifetime of the application, do note that if you set a static variable AFTER a dynamic one, the static variable's (intended) contents are not readible inside the assigning functions of a dynamic variable. (That sounded rather cryptic, so an example is in order...) <?php class ExampleClass { // assigned a static value using an expression in the constructor public $myStaticVar; // assigned a value through a function of the class public $myDynamicVar; public function __construct($myStaticVarValue) { // sets to nothing; $this->myStaticVar hasn't been assigned to yet $this->myDynamicVar = $this->setSomeValue(); // sets to 'Static Variable Value in this example $this->myStaticVar = $myStaticVarValue; } public function setSomeValue() { // returns null; $this->myStaticVar hasn't been assigned to, yet return $this->myStaticVar; } } // automatically sets $object->myStaticVar to 'Static Variable Value' //(but only after setting $object->myDynamicVar to the unassigned $object->myStaticVar) $object = new ExampleClass('Static Variable Value'); // outputs nothing print $object->myDynamicVar; ?> The above example won't print anything because you're setting the $myDynamicVar to equal the $myStaticVar, but the $myStaticVar hasn't been (technically) assigned to, yet. So, if you wish to reference that variable inside the assignment function of a dynamic variable, you have to declare it first and foremost. Otherwise, its value will not be accessible yet. The correct way... <?php class ExampleClass { // assigned a static value using an expression in the constructor public $myStaticVar; // assigned a value through a function of the class public $myDynamicVar; public function __construct($myStaticVarValue) { // sets to $myStaticVariable (or 'Some Static Variable Value' in this example) $this->myStaticVar = $myStaticVarValue; // sets to newly assigned-to $this->myStaticVariable $this->myDynamicVar = $this->setSomeValue(); } public function setSomeValue() { // returns 'Static Variable Value' return $this->myStaticVar; } } // sets $object->myStaticVar to 'Static Variable Value' automatically before $object->myDynamicVar $object = new ExampleClass('Static Variable Value'); // outputs 'Static Variable Value' print $object->myDynamicVar; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 75.76.29.219 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php Edit -- https://master.php.net/note/edit/91013 Del: integrated -- https://master.php.net/note/delete/91013/integrated Del: useless -- https://master.php.net/note/delete/91013/useless Del: bad code -- https://master.php.net/note/delete/91013/bad+code Del: spam -- https://master.php.net/note/delete/91013/spam Del: non-english -- https://master.php.net/note/delete/91013/non-english Del: in docs -- https://master.php.net/note/delete/91013/in+docs Del: other reasons-- https://master.php.net/note/delete/91013 Reject -- https://master.php.net/note/reject/91013 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#155814) next »