I have a question regarding static methods in PHP5.x. Specifically, it seems that one can access member variables declared private from static methods, just as you can from instance methods. I was wondering if this is by design, or if this "feature" might go away. I have worked up an example, hopefully it won't get mangled:

<?php

class TestClass
{
   private $myVar;
public static function loadAllObjects()
   {
       $vars = array();
       $tempVar = new TestClass();
       $tempVar->myVar = "Example";
       $vars[] = $tempVar;
return $vars; }
}


$test = TestClass::loadAllObjects();

print_r($test);


?>


This code executes fine on PHP 5.2.0 with no errors or warnings, even though one might say that I have accessed the private $myVar from "outside" the instance of TestClass created in $tempVar. This "feature" is useful to me because I'd like to create a static method on my DB-persisted objects that lets me load an array of all of a given type of object from a database, building each one without having to use setters/getters for each member variable (not because thats difficult, but because its nice to be able to have private members that can't be accessed at all from the outside world), while avoiding "lazy loading". So essentially, my questions is, is the above functionality intended?


Thanks!

-TJ

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to