This is a discussion on Class Variables and Encapsulation within the alt.comp.lang.php forums, part of the PHP Programming Forums category; "Terrence" <tom_800@hotmail.com> wrote: > Yes it is another new convert to PHP. I am ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
"Terrence" <tom_800@hotmail.com> wrote:
> Yes it is another new convert to PHP. I am dabbling in PHP classes. > Right then what we have here is one class call person with two class > variables called $firstName and $lastName. > The class has a constructor that is called from menu.php. The > constructor takes a first and last name sets the two above mention > class variables and then later on in menu.php I retrieve them using > two functions called getFirstName and getLastName. > If only this was the case! Aaaarrrg! > class Person{ > var $firstName; > var $lastName; > function Person($fName, $lName){ > /** DEFUALT VALUES *****/ > //$this->$firstName = "Peter"; > //$this->$lastName = "FRED"; > > $this->$firstName = $fName; > $this->$lastName = $lName; /** DEFAULT VALUES IN CONSTRUCTOR *****/ function Person($fName="Peter", $lName="FRED"){ $this->firstName = $fName; $this->lastName = $lName; > } > > function getFirstName(){ > return $this->$firstName; return $this->firstName; > } > > function getLastName(){ > return $this->$lastName; return $this->lastName; > } > } HTH; JOn |
|
|||
|
"Terrence" <tom_800@hotmail.com> wrote:
> Just one more question. > I see that you have omitted the dollar sign for the first and last > name class variables. > Is this because php now considers them variables by the fact that they > where declared with the "var" preceding them? Hi Terrence, Because it's the correct syntax: <quote>Note that the variable is named $cart->items, not $cart->$items, that is, a variable name in PHP has only a single dollar sign.</quote> http://uk.php.net/manual/en/language.oop.php HTH; JOn |
|
|||
|
Hi everyone,
Yes it is another new convert to PHP. I am dabbling in PHP classes. Right then what we have here is one class call person with two class variables called $firstName and $lastName. The class has a constructor that is called from menu.php. The constructor takes a first and last name sets the two above mention class variables and then later on in menu.php I retrieve them using two functions called getFirstName and getLastName. If only this was the case! Aaaarrrg! What happens is the last name is retrieved for the first name as well. I could make these variables global but that would defeat the purpose of encapsulation. Does any one have any thoughts? Many thanks Terry. /******* Menu.php **********************/ <?php require 'Person.php'; $pers = new Person("Fred", "Blogs"); ?> <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Person Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <p>This name of the persion is</p> <p>First: <?= $pers->getFirstName(); ?></p> <p>Last: <?= $pers->getLastName(); ?></p> </body> </html> /******* Person.php **********************/ <?php class Person{ var $firstName; var $lastName; function Person($fName, $lName){ /** DEFUALT VALUES *****/ //$this->$firstName = "Peter"; //$this->$lastName = "FRED"; $this->$firstName = $fName; $this->$lastName = $lName; } function getFirstName(){ return $this->$firstName; //return "Tom"; } function getLastName(){ return $this->$lastName; //return "Hanson"; } } ?> |
|
|||
|
Hi Jon,
Thanks for the response first an foremost! I was losing my will to live, phew!! Just one more question. I see that you have omitted the dollar sign for the first and last name class variables. Is this because php now considers them variables by the fact that they where declared with the "var" preceding them? Thanks Terry. "Jon Kraft" <jon@jonux.co.uk> wrote in message news:Xns94478BD95775Ejonjonuxcouk@130.133.1.4... > "Terrence" <tom_800@hotmail.com> wrote: > > > Yes it is another new convert to PHP. I am dabbling in PHP classes. > > Right then what we have here is one class call person with two class > > variables called $firstName and $lastName. > > The class has a constructor that is called from menu.php. The > > constructor takes a first and last name sets the two above mention > > class variables and then later on in menu.php I retrieve them using > > two functions called getFirstName and getLastName. > > If only this was the case! Aaaarrrg! > > > class Person{ > > var $firstName; > > var $lastName; > > function Person($fName, $lName){ > > /** DEFUALT VALUES *****/ > > //$this->$firstName = "Peter"; > > //$this->$lastName = "FRED"; > > > > $this->$firstName = $fName; > > $this->$lastName = $lName; > > /** DEFAULT VALUES IN CONSTRUCTOR *****/ > function Person($fName="Peter", $lName="FRED"){ > > $this->firstName = $fName; > $this->lastName = $lName; > > > } > > > > function getFirstName(){ > > return $this->$firstName; > > return $this->firstName; > > > } > > > > function getLastName(){ > > return $this->$lastName; > > return $this->lastName; > > > } > > } > > HTH; > JOn |