Re: Dinamically adding methods to an object instance?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Francisco M. Marzoa Alonso <fmmarzoa@xxxxxxx>:
> I've seen that's possible to add public members to objects dinamically, 
> such as:
>
> <?php
>
> class TestClass {
>     public $One=1;
> }
>
> $Obj = new TestClass ();
> $Obj->Two = 2;
>
> echo $Obj->Two;
> ?>
>
> There'll be a public "Two" member that's not defined in the class.
>
> Is it possible to add methods dinamically to an object instance in same 
> way? how? will these methods have access to private and protected 
> members of the class of the object?

You can add methods dynamically, but not in the same way that you can
add properties dynamically. You have two options:

1) Overloading the object
2) Using the classkit extension

(2) requires that you can add extensions to PHP on your server -- so,
for hosted solutions, it's out.

(1) can be accomplished in two ways, but in each, you need to setup your
class to allow overloading in the first place. 

PHP4 has the overload() function; read about it at

    http://php.net/overload

overload() sets up an environment where the magic methods __get(),
__set(), and __call() (this latter would be used to handle dynamic
methods) are utilized.

PHP5 has overloading built into the object model; see
http://php.net/oop5 for more information.

Either way, you need to add a __call() method to your class and tell it
how to handle dynamic methods.

-- 
Matthew Weier O'Phinney           | mailto:matthew@xxxxxxxxxx
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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux