Forum Moderators: coopster

Message Too Old, No Replies

Object Oriented Programming

A newbie approaches the topic

         

PartisanEntity

5:50 pm on Feb 13, 2010 (gmt 0)

10+ Year Member



Hi all,

So I would like to familiarize myself with OOP in PHP.

I have created the following script as a training example, but already I am running into trouble:

<?php

class user{

public $firstName;

public $familyName;

private $fullName;

public $gender;

public $age;

public $nationality;

/*** the constructor ***/
public function __construct(){
echo 'About this user: <br />';
}

/*** create the full name ***/
private function fullName() {

if (strlen($firstName) > 3 && strlen($familyName) > 3 ) {

$this->fullName = $firstName . ' ' . $familyName;

} else {

echo 'First or Family Name missing <br />';
}

}

public function constructUser() {

echo 'Name: '. $this->fullName . '<br />';
echo 'Gender: '. $this->gender . '<br />';
echo 'Age: '. $this->age . '<br />';
echo 'Nationality: '. $this->nationality . '<br />';

}



}

$user = new user;

$user->firstName = 'Thomas';

$user->familytName = 'Smith';

$user->gender = 'Male';

$user->age = 32;

$user->nationality = 'Austrian';

$user->constructUser();

?>


When I run the script I get:

About this user:
Name:
Gender: Male
Age: 32
Nationality: Austrian


I cant get fullName to be formed/echoed?

[edited by: PartisanEntity at 6:43 pm (utc) on Feb 13, 2010]

FourDegreez

6:31 pm on Feb 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you are not calling the fullname() function that sets the fullname member variable.

PartisanEntity

6:45 pm on Feb 13, 2010 (gmt 0)

10+ Year Member



Thanks,

I have edited the code and it works now:

class user{

public $firstName;

public $familyName;

private $fullName;

public $gender;

public $age;

public $nationality;

/*** the constructor ***/
public function __construct(){
echo 'About this user: <br />';

}

public function constructUser() {

if (strlen($this->firstName) > 3 && strlen($this->familyName) > 3) {
$this->fullName = $this->firstName . ' ' . $this->familyName;
}

echo 'Name: '. $this->fullName . '<br />';
echo 'Gender: '. $this->gender . '<br />';
echo 'Age: '. $this->age . '<br />';
echo 'Nationality: '. $this->nationality . '<br /><br />';

}



}


But what I don't understand is why it continues to work even if I delete this bit:

public $firstName;

public $familyName;

private $fullName;

public $gender;

public $age;

public $nationality;


Why would it continue to work even if I don't define the scope of these properties? (Let alone define them)?

(Just trying to get my head around OOP)

CyBerAliEn

5:10 pm on Feb 16, 2010 (gmt 0)

10+ Year Member



Unlike *most* other programming languages, PHP is very "loosely" typed. In other words...

Even though the variables were not instantiated first, you attempt to call them and set them later in the programming... so PHP will automatically create the variable.

In other languages, variables MUST be defined before they can be used or set. For example, in Java, you would have to define each of those variables. If you attempted to use the variable 'location' and it was not defined, Java would crash/error! But in PHP, it will "accept" this 'error' and automatically "fix" it for you.

So all in all, you don't have to define your variables. You could leave them all off. But... it is good practice to define every variable you will use in the class---it makes it easier to port to other languages, for others to quickly understand, etc.

PHP is incredibly forgiving in many ways. Trying creating an associated array like $fruits = array('apple'=>'red','banana'=>'yellow','orange'=>'orange'). And try doing something like: echo $fruits['grape'];

What happens? Nothing. Nothing gets printed out. You don't see an error. BUT... that key doesn't exist. A lot of languages would be VERY upset over this. What does PHP do? Nothing really. All it does do, is it enters a 'NOTICE' level error in the logs (saying the key does not exist). Most hosting have NOTICE level errors turned off, so most people never even know this type of stuff happens. Even though you shouldn't try to access a key that doesn't exist (or a variable that hasn't been set/defined; etc)... PHP will let you do these types of things without noticeable error (although it will usually note it as a 'minor error' in the logs).