Re: [PHP] Static methods have access to private members

2007-05-17 Thread Miguel J. Jiménez

Theodore Root escribió:
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

Mmm pardon me if I am wrong but I think you have accessed the private 
variable using an instanced object inside the static method and what you 
are returning is fine... You did not access the private variable using 
$this-myVar ...


--
Miguel J. Jiménez
Programador Senior
Área de Internet/XSL/PHP
[EMAIL PROTECTED]



ISOTROL
Edificio BLUENET, Avda. Isaac Newton nº3, 4ª planta.
Parque Tecnológico Cartuja '93, 41092 Sevilla (ESP).
Teléfono: +34 955 036 800 - Fax: +34 955 036 849
http://www.isotrol.com

You let a political fight  come between you and your best friend you have in all 
the world. Do you realize how foolish that is? How ominous? How can this country survive 
if friends can't rise above the quarrel.
Constance Hazard, North  South (book I)


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

[PHP] Static methods have access to private members

2007-05-16 Thread Theodore Root
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