PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Capitalization and Classes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Capitalization and Classes

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 20h37   #1
Good Man
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Capitalization and Classes

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;
}

}

  Réponse avec citation
Vieux 06/11/2007, 20h43   #2
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capitalization and Classes

..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
  Réponse avec citation
Vieux 06/11/2007, 20h44   #3
Good Man
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capitalization and Classes

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.
  Réponse avec citation
Vieux 06/11/2007, 20h53   #4
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capitalization and Classes

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()


  Réponse avec citation
Vieux 06/11/2007, 21h11   #5
Good Man
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capitalization and Classes

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



  Réponse avec citation
Vieux 07/11/2007, 01h50   #6
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capitalization and Classes

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!

  Réponse avec citation
Vieux 07/11/2007, 15h35   #7
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Capitalization and Classes


"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.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h34.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16508 seconds with 15 queries