Well may be i'm the only one (hard to say as nobody talk about this) but 
i don't agree with accessors design, i allready posted a mail to Andrei 
a month ago (may be lost, may be the wrong person) and i try here as 
i'm sure someone will explain me 'why this design'.

Comment found in zend_object_handlers.c :

/*
  __X accessors explanation:

if we have __get and property that is not part of the properties array 
is requested, we call __get handler. If it fails, we return 
uninitialized.

if we have __set and property that is not part of the properties array 
is set, we call __set handler. If it fails, we do not change the array.

for both handlers above, when we are inside __get/__set, no further 
calls for __get/__set for these objects will be made, to prevent 
endless recursion and enable accessors to change properties array.

if we have __call and method which is not part of the class function 
table is called, we cal __call handler.
*/


--------------- here we go ---------------------------------------------

The way python handle this is just fine and allow much more code magic 
in classes.


The python rule : if we have a __get, __set, __call, we use it. no more 
no less.



What i expect from my __set by example :

/**
 * My magic class Foo 
 */
class Foo {

        /** 
         * bar is visible in the interface, 
         * declared public, 
         * no documentation problem 
         * the user know that he can use it safely
         */
        var $bar;

        /**
         * Hidden from user, he don't have to worry about what's behind
         * the scene.
         */
        function __set($var, $value)
        {
                if ($var == 'bar') {
                        // ... 
                        // if $value is an id, we use it to load bar
                        // object from database and do complex stuff
                        
                        // ...
                        // else if $value is an object conform
                        // to what 'bar' variable waits for we save 
                        // it to database, mail to the president, send
                        // a teddy bear to mars.

                        // now $value was modified to fit $this->bar 
                        // requirements the last line of this function will 
                        // set it 
                }

                // here i don't use $this->$var = $value because i'm a 
                // carefull coder who know what he does and who know 
                // what he doesn't want (no infinite call to __set)
                // but if for some advanced magic i'd like to do it
                // i could and it's cool.
                object_set_var($this, $var, $value);
        }
};

and to be sure this design will be usuable, php would produce 3 
functions with references everywhere:
 
  object_set($object, $varName, $value)
  object_get($object, $varName)
  object_call($object, $method, $arguments[])

tada ! python way of coding : clean, simple, powerfull within your 
favorite web scripting language.



Of course this thing is possible with the current system but it requires 
the following :

1 - do not declare 'var $bar' or declare it private (need some more test 
for private but not the right solution as the magic requires public 
access). 

2 - create a private hashtable which will handle undeclared variables 
(because once a variable is set in the object's properties array, __get 
and __set won't work anymore with it)

3 - create the __get() method so we can retrieve the variable from the 
private hashtable.

4 - disable error_reporting(E_ALL) because $object->myvar is not 
declared and you can't ask it in your code.


Well, all this will make the code uggly, unclear to document, hard to 
maintain, and will take anybody about 3 times the same to develop the 
above example... And of course you're limited as you can't call other 
variables setters while in __set (same for __get and __call)!


To finish with, i hope you'll teach me advantages of the current design 
because i took a look at the zend_object_handlers.c and ... well ... i 
really think there's a 5:00 am conception mistake behind the scene :)



also on this mailing list if you want more bugs report and features 
request :

- where are my modules ? (something like python or perl module 
import/use is just great) 

- when will "use()" be implemented (a package attempt i bet) ?

- why should 'import' complain if i try to import a class already 
imported in the current namespace ? The problem will occurs quite often 
with library inclusion and cross references.

- get_class_methods() do not return methods array when used on a 
namespaced class 

- do you plain to allow : "import class $className from $nameSpaceName;" 
? i do this kind of things often don't ask me why, i just like plugin 
way of life.

- will import include my php files if they are well named (module 
convention) ? ( i mean in final version of ZendEngine2 )

- why didn't i use the qa system to relate latests bugs ? sorry for 
this... but sounds the better place for this kind of question


best regards, and i hope this will help for ZendEngine2
Laurent

      ( i'm french - english errors are not intentional )




-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to