$name and name ?? considered same by PHP ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

$name and name ?? considered same by PHP ?

Post 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 ????
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

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