473,813 Members | 3,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_exist s($this,$method )) {
return $this->$method();
}
}

function getName() {
return "Bob";
}

function getAge() {
return 44;
}

}

Nov 6 '07 #1
6 1489
..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.de wrote in
news:0t******** *************** *********@4ax.c om:
.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.c omwrote:
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_exist s($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.comwro te in
news:11******** ************@k7 9g2000hse.googl egroups.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_exist s($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.c omwrote:
Darko <darko.maksimo. ..@gmail.comwro te innews:11****** **************@ k79g2000hse.goo glegroups.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_exist s($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.comwro te in message
news:11******** **************@ 22g2000hsm.goog legroups.com...
On Nov 6, 9:11 pm, Good Man <he...@letsgo.c omwrote:
>Darko <darko.maksimo. ..@gmail.comwro te
innews:11***** *************** @k79g2000hse.go oglegroups.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_exist s($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
6206
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
1300
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, the editor behaves as if I had set it to "Lowercase". Is this a bug or what? How can I simply enter xml tags without being either uppercased or lowercased?
8
1984
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 "object", and the mother of all exception "Exception" (with capital E)? Why is not object spelled "Object" then? Especially since Exception's descendants are named with the first letter of each word capitalized? I mean, in Java, it's Object....
6
10246
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 that I reference this button in VBA code, the Visual Basic Editor capitalizes the P changing btnAlbumUp.SetFocus to
2
9509
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 to do that? Thanks. Tsung-Yu
18
2049
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. Yet, in my understanding, attributes are only __gc and __value class specific and do not apply to __nogc classes. Is this correct ? If so, how is the packing alignment of __nogc classes stored ?
5
1597
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 casing? Is there a right way and a wrong way? I believe that the W3C recommends lower casing. Thanks for your help.
6
2945
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 calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
0
2035
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 this topic getting lost before people interested in the problem notice it. The important tricks to the solution are two: 1) make the custom classes take a TEMPLATE argument which defines their BASE class 2) EMBED the custom classes in a "Traits"...
2
1808
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 classes are usually written in lowercase, like foo in ``foo = CamelCase()``. So why are True and False (instances of bool) capitalized? Shouldn't they be "true" and "false"? Same goes for None.
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10665
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.