note 73200 added to language.oop5.paamayim-nekudotayim

From: Date: Tue, 13 Feb 2007 08:11:27 +0000
Subject: note 73200 added to language.oop5.paamayim-nekudotayim
Groups: php.notes 
Request: Send a blank email to php-notes+get-123303@lists.php.net to get a copy of this message
This is a solution for those that still need to write code compatible with php 4 but would like to use the flexibility of static variables. PHP 4 does not support static variables within the class scope but it does support them within the scope of class methods. The following is a bit of a workaround to store data in static mode in php 4. Note: This code also works in PHP 5. (Tested on version 4.3.1+) The tricky part is when using when arrays you have to do a bit of fancy coding to get or set individual elements in the array. The example code below should show you the basics of it though. <?php class StaticSample { //Copyright Michael White (www.crestidg.com) 2007 //You may use and modify this code but please keep this short copyright notice in tact. //If you modify the code you may comment the changes you make and append your own copyright //notice to mine. This code is not to be redistributed individually for sale but please use it as part //of your projects and applications - free or non-free. //Static workaround for php4 - even works with arrays - the trick is accessing the arrays. //I used the format s_varname for my methods that employ this workaround. That keeps it //similar to working with actual variables as much as possible. //The s_ prefix immediately identifies it as a static variable workaround method while //I'm looking thorugh my code. function &s_foo($value=null, $remove=null) { static $s_var; //Declare the static variable. The name here doesn't matter - only the name of the method matters. if($remove) { if(is_array($value)) { if(is_array($s_var)) { foreach($value as $key => $data) { unset($s_var[$key]); } } } else { //You can't just use unset() here because the static state of the variable will bring back the value next time you call the method. $s_var = null; unset($s_var); } //Make sure that you don't set the value over again. $value = null; } if($value) { if(is_array($value)) { if(is_array($s_var)) { //$s_var = array_merge($s_var, $value); //Doesn't overwrite values. This adds them - a property of the array_merge() function. foreach($value as $key => $data) { $s_var[$key] = $data; //Overwrites values. } } else { $s_var = $value; } } else { $s_var = $value; } } return $s_var; } } echo "Working with non-array values.<br>"; echo "Before Setting: ".StaticSample::s_foo(); echo "<br>"; echo "While Setting: ".StaticSample::s_foo("VALUE HERE"); echo "<br>"; echo "After Setting: ".StaticSample::s_foo(); echo "<br>"; echo "While Removing: ".StaticSample::s_foo(null, 1); echo "<br>"; echo "After Removing: ".StaticSample::s_foo(); echo "<hr>"; echo "Working with array values<br>"; $array = array(0=>"cat", 1=>"dog", 2=>"monkey"); echo "Set an array value: "; print_r(StaticSample::s_foo($array)); echo "<br>"; //Here you need to get all the values in the array then sort through or choose the one(s) you want. $all_elements = StaticSample::s_foo(); $middle_element = $all_elements[1]; echo "The middle element: ".$middle_element; echo "<br>"; $changed_array = array(1=>"big dog", 3=>"bat", "bird"=>"flamingo"); echo "Changing the value: "; print_r(StaticSample::s_foo($changed_array)); echo "<br>"; //All you have to do here is create an array with the keys you want to erase in it. //If you want to erase all keys then don't pass any array to the method. $element_to_erase = array(3=>null); echo "Erasing the fourth element: "; $elements_left = StaticSample::s_foo($element_to_erase, 1); print_r($elements_left); echo "<br>"; echo "Enjoy!"; ?> ---- Server IP: 194.153.94.10 Probable Submitter: 72.15.123.32 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php Edit -- https://master.php.net/note/edit/73200 Del: integrated -- https://master.php.net/note/delete/73200/integrated Del: useless -- https://master.php.net/note/delete/73200/useless Del: bad code -- https://master.php.net/note/delete/73200/bad+code Del: spam -- https://master.php.net/note/delete/73200/spam Del: non-english -- https://master.php.net/note/delete/73200/non-english Del: in docs -- https://master.php.net/note/delete/73200/in+docs Del: other reasons-- https://master.php.net/note/delete/73200 Reject -- https://master.php.net/note/reject/73200 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123303) next »