?
Solved

I do not understand why properties are declared.

Posted on 2013-12-03
5
Medium Priority
?
265 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses

765 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