Page 1 of 1

$name and name ?? considered same by PHP ?

Posted: Tue Nov 25, 2014 7:06 am
by gautamz07
Below is my object oriented programme to display a persons name :

Code: Select all

[syntax=php]
<?php

require 'person.php';

$person = new person;

$person->name = "Gautam";

$person->age = 17;

echo $person->details(); 

?>	
[/syntax]

and the person.php file :

Code: Select all

<?php

/**
* 
*/
class Person
{
	
	public $name = "Ketan";

	public $age = 19;

	function details()
	{
		return $this->name . ' is ' . $this->age . ' years old '; 
	}
}



?>
now in the index file if i comment out the below two lines :

Code: Select all

$person->name = "Gautam";

$person->age = 17;
when i run the programme i get the following output :

Code: Select all

Ketan is 19 years old
now how is that possible when in the person file i have the below :

Code: Select all

return $this->name . ' is ' . $this->age . ' years old '; 
and i have delared this :

Code: Select all

	public $name = "Ketan";

	public $age = 19;
now is $name and name the same thing ????

Re: $name and name ?? considered same by PHP ?

Posted: Tue Nov 25, 2014 9:41 am
by Celauran
$this->name refers to the $name property of the class or, more specifically, to the specific instance of the class. You set the defaults to be Ketan and 19, so the output you're seeing is indeed the expected behaviour.