|
Posted by Steve on 11/07/07 14:35
"Darko" <darko.maksimovic@gmail.com> wrote in message
news:1194396610.447598.279570@22g2000hsm.googlegroups.com...
> On Nov 6, 9:11 pm, Good Man <he...@letsgo.com> wrote:
>> Darko <darko.maksimo...@gmail.com> wrote
>> innews:1194378798.505407.6390@k79g2000hse.googlegroups.com:
>>
>>
>>
>> >> print $p->name;
>> >> print $p->Name;
>>
>> >> The method in the class is getName, so why would "name" (lowercase)
>> >> work here? All my experience in PHP says that uppercase/lowercase is
>> >> vitally important!
>>
>> >> Here's the class:
>>
>> >> class Person {
>>
>> >> function __get($property) {
>> >> $method = "get{$property}";
>> >> if(method_exists($this,$method)) {
>> >> return $this->$method();
>> >> }
>> >> }
>>
>> >> function getName() {
>> >> return "Bob";
>> >> }
>>
>> >> function getAge() {
>> >> return 44;
>> >> }
>>
>> >> }
>>
>> > It's impossible that works. You probably meant $p->getname() and $p-
>> >>getName()
>>
>> Promise you that it does :)
>>
>> It's all about the __get($property) Interceptor Method :)
more specifically:
$method = "get{$property}";
but, this is a pretty hoaky way of hacking php to act like it has built-in
getters/setters. __get only ever gets called when php CAN'T determine the
interface name...NOT when you call a defined interface like getName(). this
method predicates that all developers programming this class knows the magic
and that the (readable) interfaces they create begin with 'get' and that
they take NO parameters. this not only effects the developer but the caller
as well. the caller must know that $obj->foo('param') will fail to do
anything since the magic code above will die a painful death if 'param' is
required and not optional...via $this->$method();. consider that you too had
to initially think 'why in the hell would that work and where does Name come
from'. you do NOT want people asking those questions when looking/using your
code, especially when it's another developer...or your boss.
better to be explicit in object architecture and consumption. until php
formally provides built-in getters/setters for interfaces THAT ARE DEFINED,
i'd consider it an entire waste of time trying to fudge it using
__get/__set. stay away from voodoo.
[Back to original message]
|