Class Variables and Encapsulation

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


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-04-2003
Jon Kraft
 
Posts: n/a
Default Re: Class Variables and Encapsulation

"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
Reply With Quote
  #2 (permalink)  
Old 12-04-2003
Jon Kraft
 
Posts: n/a
Default Re: Class Variables and Encapsulation

"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
Reply With Quote
  #3 (permalink)  
Old 12-04-2003
Terrence
 
Posts: n/a
Default Class Variables and Encapsulation

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


Reply With Quote
  #4 (permalink)  
Old 12-04-2003
Terrence
 
Posts: n/a
Default Re: Class Variables and Encapsulation

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



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:44 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0