Capitalization and Classes
Capitalization and Classes
am 06.11.2007 20:37:23 von Good Man
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;
}
}
Re: Capitalization and Classes
am 06.11.2007 20:43:12 von Michael Fesser
..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
Re: Capitalization and Classes
am 06.11.2007 20:44:00 von Good Man
Michael Fesser 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.
Re: Capitalization and Classes
am 06.11.2007 20:53:18 von darko
On Nov 6, 8:37 pm, Good Man 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()
Re: Capitalization and Classes
am 06.11.2007 21:11:14 von Good Man
Darko wrote in
news: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 :)
Re: Capitalization and Classes
am 07.11.2007 01:50:10 von darko
On Nov 6, 9:11 pm, Good Man wrote:
> Darko 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 :)
Right! Those fancy new methods I chose not to read when reading the
modern php manual. Knew I would regret! :)
Re: Capitalization and Classes
am 07.11.2007 15:35:04 von Steve
"Darko" wrote in message
news:1194396610.447598.279570@22g2000hsm.googlegroups.com...
> On Nov 6, 9:11 pm, Good Man wrote:
>> Darko 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.