On Aug 26, 2005, at 5:55 AM, Derick Rethans wrote:

I'm just arguing that the current way that setters and getters are
implemented is broken. Instead of keeping a broken behavior I would like
to see it fixed.

Derick,

It is not broken its incomplete. PHP doesn't really have an implementation of getters and setters. The __get and __set are really just "property not defined" hooks, not getters and setters (accessor methods).

If you take a look at a dynamic language that does does define a standard for getters and setters, Key Value Coding in Objective C, you will see what I mean. KVC defines a (complicated) search path for looking up a property:

1. Search the receiver’s class for an accessor method whose name matches the pattern -get<Key>, -<key>, or -is<Key>, in that order. 2. If no accessor is found, and the receiver’s class method accessInstanceVariablesDirectly returns YES, the receiver is searched for an instance variable whose name matches the pattern _<key>, _is<Key>, <key>, or is<Key>, in that order. 3. If no appropriate accessor or instance variable is found, valueForUndefinedKey: is invoked for the receiver, and the value is returned.

ObjectiveC's valueForUndefinedKey is the same as PHP's __get. What php lacks is the stage in the process that looks for accessor methods. In current PHP:

1. Search the object for an instance variable <key>
2. If no instance variable is found, __get is invoked on the object and the value returned.

I think where you are running into trouble with the property keyword is that it is targeting the wrong stage in the property name search process. It simply does not make sense to declare properties in the class for the "undefined property" (__get) stage of the search process.

Right now, most people simulate accessor methods in user space using __get and __set and if statements, case statements, introspection, arrays, or a variety of other techniques.

If you want to improve this process and bring it into the engine, then the place to do it is the property name search path. This is where declared properties with accessor methods should be handled. An accessor method check could be added either before or after the check for the instance variable. A new keyword isn't necessary here, a simple naming convention could suffice as in ObjectiveC. Reflection could be made aware of the naming conventions, etc.

However, adding the stage before, as in ObjectiveC, seems unlikely to gain much support here because of potential BC issues with method names and performance concerns. (Although, some things will become faster, some things will become slower.)

Adding the stage after also seems unlikely to gain much support here because it can be simulated fairly well right now in user space using the current __get mechanisms. (Which is exactly what we do on the WACT project.)

I happen to think that a PHP standard for accessor methods would be a good thing, but adding a stage to the search path is a hard sell. Without adding a stage to the search path for property names, a property keyword such as proposed makes little sense, the current mechanisms are sufficient.

I understand the problem you're trying to solve, support the need for a solution, but don't think this proposal solves the problem.

Ref:
http://developer.apple.com/documentation/Cocoa/Conceptual/ KeyValueCoding/index.html
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to