Re: [PHP] Re: Add methods to object dinamically
On Tue, 2004-10-26 at 14:21, Francisco M. Marzoa wrote: > Greg Beaver wrote: > > > Sort of :) > > > > In PHP5, this will work. > > [...] > > Very nice, thanks a lot :-) > > > > > However, you won't have access to private or protected data members. > > Damn! that's exactly what I want! :-( > > > It is always better to rigidly define your methods, and extend the > > class as someone else suggested. > > The problem is that I need to serialize some objects to put them on a > database, but I'll have problems when some of these objects contains > another ones from third parties where I do not control the inheritance > and I've not access directly to object fields. > > Converting the object to an array is useful for serialization, as I can > access private and protected object member values, but I cannot > unserialize them because I cannot wrote those members again. > > Never mind. I surely must rethink the matter again. > > Thanks a million, Greg. Could you serialize the object then parse the serialized data yourself and manipulate the values as an array, then re-serialize back to object form and then unserialize as an object with modified protected/private members? Sounds like a lot of work, but it just might bypass the whole protected/private issue :) Not that it's generally a good thing to bypass such things *heheh*. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Add methods to object dinamically
Greg Beaver wrote: Sort of :) In PHP5, this will work. [...] Very nice, thanks a lot :-) However, you won't have access to private or protected data members. Damn! that's exactly what I want! :-( It is always better to rigidly define your methods, and extend the class as someone else suggested. The problem is that I need to serialize some objects to put them on a database, but I'll have problems when some of these objects contains another ones from third parties where I do not control the inheritance and I've not access directly to object fields. Converting the object to an array is useful for serialization, as I can access private and protected object member values, but I cannot unserialize them because I cannot wrote those members again. Never mind. I surely must rethink the matter again. Thanks a million, Greg. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Add methods to object dinamically
Francisco M. Marzoa Alonso wrote: I've seen that's possible to add members to objects dinamically, in example: class MyClass { private $a; } $MyObject = new MyClass (); $MyObject->b = 1; Now $MyObject has a public member called 'b' that has a value of '1'. The question is, is it possible to add methods in the same way? I meant something such as: function sum ( $this ) { return $this->a+$this->b; } $MyObject->sum = sum; Note that code will not work, but it illustrates what I'm asking for. Sort of :) In PHP5, this will work. _methods[$method])) { $call = $this->_methods[$method]; return $call($this); } } public function __set($var, $value) { if (is_string($value) && is_callable($value)) { $this->_methods[$var] = $value; } else { $this->$var = $value; } } } function test($o) { return $o->val; } $o = new DoesStuff; $o->val = 4; $o->test = 'test'; echo $o->test(); ?> However, you won't have access to private or protected data members. It is always better to rigidly define your methods, and extend the class as someone else suggested. Greg -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Add methods to object dinamically
* Francisco M. Marzoa Alonso <[EMAIL PROTECTED]>: > I've seen that's possible to add members to objects dinamically, in example: > > class MyClass { > private $a; > } > > $MyObject = new MyClass (); > $MyObject->b = 1; > > Now $MyObject has a public member called 'b' that has a value of '1'. > > The question is, is it possible to add methods in the same way? > > I meant something such as: > > function sum ( $this ) { > return $this->a+$this->b; > } > > $MyObject->sum = sum; No. There are some things you can look into, however. PHP4 has an overload() function (and PHP5 has it built in) -- you could then add a __call() method that checks to see if a function is defined, and if so, calls it. class MyClass { var $a; // Note: you'd call this differently in PHP5 function __call($function, $arguments, &$return) { if (function_exists($function)) { $function($arguments); } } } overload('MyClass'); // This line unncessary in PHP5 The other possibility is to look into the classkit extension (http://php.net/classkit); this would require being able to install PHP extensions (not a problem if it's your own box). -- Matthew Weier O'Phinney | mailto:[EMAIL PROTECTED] Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php