Solved

I do not understand why properties are declared.

Posted on 2013-12-03
5
250 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
Comment Utility
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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 166 total points
Comment Utility
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
Comment Utility
Okay
for organization
and security (I do not understand yet)

Thank you.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now