[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

I do not understand why properties are declared.

Posted on 2013-12-03
5
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 668 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 668 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 111

Accepted Solution

by:
Ray Paseur earned 664 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 111

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 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