note 58724 added to keyword.paamayim-nekudotayim

From: Date: Mon, 14 Nov 2005 06:05:43 +0000
Subject: note 58724 added to keyword.paamayim-nekudotayim
Groups: php.notes 
Request: Send a blank email to php-notes+get-98529@lists.php.net to get a copy of this message
Since re-posting a simplified version of the peterjoel.com solution to class variables in php4 was kinda lame, here's the modified solution to give you one function (per class) that allows you to use any number of 'class' variables... <?php class theClass { function &classVars($name = NULL, $value = NULL) { // The classVars function is (possibly) returning a reference... static $myClassVars; // Define a static var to hold our 'class' vars... if (!is_array($myClassVars)) { // Initialize the array if it isn't already... $myClassVars = array(); } if (!is_null($name) && !is_null($value)) { // If we got two arguments, set the class var that was passed... $myClassVars[$name] = $value; } if (!is_null($name)) { // If we got one argument, return the var it's referring to... return($myClassVars[$name]); } else { return(NULL); } } } // Setting class variables... theClass::classVars("varOne", "valueOne"); theClass::classVars("varTwo", "valueTwo"); // Getting class variables... $myRef = &theClass::classVars("varOne"); echo theClass::classVars("varTwo"); ?> You should be able to store anything in your class vars this way, and depending on how you call the function, you can have either a copy or a reference to the class var returned (see manual on 'returning by reference'...) Again, hope this helps someone... ---- Manual Page -- http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58724 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58724&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58724&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58724&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58724&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58724&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58724&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58724&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58724&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98529) next »