[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

I do not understand why properties are declared.

<?php
class Student{
 private $name;
 public $age;
 public function __construct($name,$age){
   $this->name=$name;
   $this->age=$age;
   $this->sum=$name+$age;
 }
}
$tmpStudent=new Student("Mindi","22");
echo "Age : ". $tmpStudent->age;
echo "<br>Sum:".$tmpStudent->sum;

Open in new window



I am typing from a code tutorial.

I think a good way of learning is by adding lines to the existing code.

So I added $this->sum which still displayed but did not need to be declared as a property above the constructor.

Why are properties declared?
0
rgb192
Asked:
rgb192
3 Solutions
 
gr8gonzoConsultantCommented:
Basically, because it's the right way to do it.

PHP may ALLOW you to add properties on the fly, but you can't define whether the property scope (public/protected/private), and it makes the class definition harder to read.

If you don't declare it ahead of time, then the property is only initialized the first time it is written.

Unless there's a REALLY good reason to define all of your properties on the fly instead of declaring them beforehand, you should always try to declare your properties. You'll have more control over them, you'll adhere to best coding practices, and your code will be easier to read and maintain.
0
 
Loganathan NatarajanLAMP DeveloperCommented:
Actually, declaring properties for the class is more standard way (as per JAVA oops),  and you should declare it  private, public and protected.   in fact any variable to use, it must be declared in advance other wise it will lead to security loop hold ... I hate if the properties are not declared for any class ...
0
 
Ray PaseurCommented:
Properties that are created on the fly are, by definition, public.  As are properties that are injected into the object (an inherently bad idea -- just one of many PHP oddities).  Thoughtful design often uses getter() and setter() methods.  In this way you can make your properties be protected, which is usually a good starting point, absent any strong reason to make them public or private.
http://www.laprbass.com/RAY_temp_rgb192.php

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28309711.html

class Student{
 private $name;
 public $age;
 public function __construct($name,$age){
   $this->name=$name;
   $this->age=$age;
   $this->sum=$name+$age;
 }
}
$tmpStudent=new Student("Mindi","22");
echo "Age : ". $tmpStudent->age;
echo "<br>Sum:".$tmpStudent->sum;

// SURPRISE!
$tmpStudent->foo = 'UNWANTED INJECTION';
$tmpStudent->age = 33;

// SHOW THE NEWLY DAMAGED OBJECT
var_dump($tmpStudent);

// SHOW THAT THE NEWLY INJECTED "AGE" HAS NOT BEEN USED IN THE COMPUTATION OF THE "SUM"
echo "<br>Sum:".$tmpStudent->sum;

Open in new window

0
 
rgb192Author Commented:
Okay
for organization
and security (I do not understand yet)

Thank you.
0
 
Ray PaseurCommented:
Sorry, I forgot to add one other reason to declare your properties... You control the order of their appearance in the output from var_dump() and print_r().  This can matter if you have a large number of properties!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now