tags:

views:

73

answers:

4

Im new to PHP Object Oriented Programming but I know to code in procedural way.

If this is my PHP Class

<?php
class person {
  var $name;
  function __construct($persons_name) {
   $this->name = $persons_name;
  }

  function get_name() {
     return $this->name;
  }
}
?>

and if I access it in my PHP page

$jane = new person("Jane Doe");
echo "Her name is : ".$jane->get_name();

Question: Is it really necessary to put the var $name; in my PHP class since I can correctly get an output of Her name is : Jane Doe even without the var $name; in my PHP class?

+2  A: 

Semantically, you should, as $name is indeed an attribute of your class. Your constructor already assigns $persons_name to the attribute, but if you left the var $name; out, the rest of your script wouldn't really know that there's such an attribute in your class. Additionally if your constructor didn't assign it right away, person::get_name() would attempt to retrieve an undeclared $name attribute and trigger a notice.

As Brenton Alker says, declaring your class attributes explicitly allows you to set their visibility. For instance, since you have get_name() as a getter for $name, you can set $name as private so a person's name can't be changed from outside the class after you create a person object.

Also, attempting to assign to undeclared class attributes causes them to be declared as public before being assigned.

BoltClock
A: 

it is very useful. imagine you write a bunch of person objects with different names in an array and you will call the names of all objects at another place in your website. this is just possible when you store the name of every particular person.

tagtraeumer
Can you give a sample code where it will be very useful, since Im new to OOP. Thanks
Sanny
sure: <?php function getPersons() { $sql = "SELECT name FROM person WHERE something = 'somewhat'"; $result = mysql_query($sql); $persons = array(); while ($row = mysql_fetch_assoc($result)) { $persons[] = new Person($row["name"]); } return $persons; } ?>when u call this function you get the personarray. you need the stored name to identify all persons.(i'm not really in php atm, so there may be some syntactical mistakes, but it will describe what i mean)
tagtraeumer
sry I don't know how to put code in this kind of comment
tagtraeumer
A: 
fabrik
+3  A: 

Not technically, no, PHP is very forgiving to things like this. But it is good practice, if only for documentation purposes.

Probably more importantly, declaring the property explicitly lets you set its visibility. In your example $name is a public property (public is the default visibility in PHP). If you don't need it to be public (it's often safer not to, due to getters/setters allowing better control over what values can be assigned) then you should declare if protected or private.

Brenton Alker
1 more question sir:is $this->name in the functions "related" to var $name; or they are 2 different variables
Sanny
I mean the is the reason for declaring var $name is because there is $this->name inside the functions?
Sanny
Yes, $this->name is referencing the ($next) property of the current instance ($this).
Brenton Alker