1

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?

StasM
  • 10,593
  • 6
  • 56
  • 103
Sanny
  • 45
  • 2
  • *(related)* [Learning PHP Class](http://stackoverflow.com/questions/2206387/learning-php-class/2206835) – Gordon Jul 20 '10 at 06:37

4 Answers4

4

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
  • 8,947
  • 3
  • 36
  • 37
  • 1 more question sir: is $this->name in the functions "related" to var $name; or they are 2 different variables – Sanny Jul 20 '10 at 06:43
  • I mean the is the reason for declaring var $name is because there is $this->name inside the functions? – Sanny Jul 20 '10 at 06:44
  • Yes, $this->name is referencing the ($next) property of the current instance ($this). – Brenton Alker Jul 20 '10 at 06:50
2

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.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

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
  • 1,451
  • 11
  • 19
  • Can you give a sample code where it will be very useful, since Im new to OOP. Thanks – Sanny Jul 20 '10 at 06:25
  • sure: 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 Jul 20 '10 at 06:39
0

If i understand correctly, you don't need to declare var $name. You can use PHP's magic methods instead, in this case __set and __get.

Edit: there's a small introduction about magic methods @ Nettuts.

fabrik
  • 14,094
  • 8
  • 55
  • 71