Solved

I do not understand why properties are declared.

Posted on 2013-12-03
5
256 Views
Last Modified: 2013-12-04
<?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
Comment
Question by:rgb192
5 Comments
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 167 total points
ID: 39694533
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
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 167 total points
ID: 39694622
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 166 total points
ID: 39695372
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
 

Author Closing Comment

by:rgb192
ID: 39695452
Okay
for organization
and security (I do not understand yet)

Thank you.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39695476
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question