|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi there
I'm learnin' myself some OO programming (finally), and was wondering if someone could briefly tell me why when setting up my object... $p = new Person(); .... that the following both print "Bob" to the screen: 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; } } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
..oO(Good Man)
>I'm learnin' myself some OO programming (finally), and was wondering if >someone could briefly tell me why when setting up my object... > >$p = new Person(); > >... that the following both print "Bob" to the screen: > >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! Variable names are case-sensitive, method names are not. Micha |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Michael Fesser <netizen@gmx.de> wrote in
news:0tg1j3d60a4ut617q8c0g87fjnqsta9hmb@4ax.com: > .oO(Good Man) > >>I'm learnin' myself some OO programming (finally), and was wondering if >>someone could briefly tell me why when setting up my object... >> >>$p = new Person(); >> >>... that the following both print "Bob" to the screen: >> >>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! > > Variable names are case-sensitive, method names are not. Many thanks. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Nov 6, 8:37 pm, Good Man <he...@letsgo.com> wrote:
> Hi there > > I'm learnin' myself some OO programming (finally), and was wondering if > someone could briefly tell me why when setting up my object... > > $p = new Person(); > > ... that the following both print "Bob" to the screen: > > 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() |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Darko <darko.maksimovic@gmail.com> wrote in
news:1194378798.505407.6390@k79g2000hse.googlegrou ps.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 ![]() |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Nov 6, 9:11 pm, Good Man <he...@letsgo.com> wrote:
> Darko <darko.maksimo...@gmail.com> wrote innews:1194378798.505407.6390@k79g2000hse.googlegr oups.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 ![]() Right! Those fancy new methods I chose not to read when reading the modern php manual. Knew I would regret! ![]() |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
"Darko" <darko.maksimovic@gmail.com> wrote in message news:1194396610.447598.279570@22g2000hsm.googlegro ups.com... > On Nov 6, 9:11 pm, Good Man <he...@letsgo.com> wrote: >> Darko <darko.maksimo...@gmail.com> wrote >> innews:1194378798.505407.6390@k79g2000hse.googlegr oups.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. |
|
![]() |
| Outils de la discussion | |
|
|