473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

}

Nov 6 '07 #1
6 1476
..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
Nov 6 '07 #2
Michael Fesser <ne*****@gmx.dewrote in
news:0t********************************@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.
Nov 6 '07 #3
On Nov 6, 8:37 pm, Good Man <he...@letsgo.comwrote:
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()
Nov 6 '07 #4
Darko <da**************@gmail.comwrote in
news:11********************@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 :)

Nov 6 '07 #5
On Nov 6, 9:11 pm, Good Man <he...@letsgo.comwrote:
Darko <darko.maksimo...@gmail.comwrote innews:11********************@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! :)

Nov 7 '07 #6

"Darko" <da**************@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
On Nov 6, 9:11 pm, Good Man <he...@letsgo.comwrote:
>Darko <darko.maksimo...@gmail.comwrote
innews:11********************@k79g2000hse.googleg roups.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.
Nov 7 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: icrash2 | last post by:
Hi, I'm using PHP to submit form input to MySQL and need a way to capitalize the first character for certain fields. Does anyone know of some way to achieve this? Much appreciated
0
by: Agoston Bejo | last post by:
Tools / Options / Text Editor / HTML/XML / Format / Capitalization Usually I use Tags: Uppercase; Attributes: Uppercase but now I edit XML pages and I set them both to "As entered". However,...
8
by: Ray | last post by:
Hello guys, OK, I've been reading some more about Python. There are some things about Python exception that I haven't been able to grasp: 1. This is a small thing, but why is object spelled...
6
by: Corepaul | last post by:
I am new to Access 2000. My operating system is Windows 2000. In the early stage of development I noticed something weird. On my form, I have a Command Button named "btnAlbumUp". The first time...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
5
by: Bruce W.1 | last post by:
Looking at the html produced by Visual Studio I see a mixed bag of tag capitalization. For example there's <HEAD> and <body>. Then there's <form and <TABLE>. Is there some logic behind the mixed...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
2
by: cbtube03 | last post by:
I see that naming conventions are such that classes usually get named CamelCase. So why are the built-in types named all lowercase (like list, dict, set, bool, etc.)? And names for instances of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.