tags:

views:

2089

answers:

5

I'm new to the OOP paradigm, so there's probably a simple explanation for this question...

Do you always need to declare public object-wide variables in a class? For example:

<?php

class TestClass
{
    var $declaredVar;

    function __construct()
    {
     $this->declaredVar = "I am a declared variable.";
     $this->undeclaredVar = "I wasn't declared, but I still work.";
    }

    function display()
    {
     echo $this->declaredVar . "<br />";
     echo $this->undeclaredVar;
     echo "<br /><br />"; 
    }
}

$test = new TestClass;
$test->display();

$test->declaredVar = "The declared variable was changed.";
$test->undeclaredVar = "The undeclared variable was changed.";

$test->display();

?>

In this code, even though $declaredVar is the only declared variable, $undeclaredVar is just as accessible and useable--it seems to act as if I had declared it as public.

If undeclared class variables are always accessible like that, what's the point of declaring them all up front?

Thanks!

+2  A: 

General OOP paradigm of encapsulation says you should not expose your inner state variables out side that means they should be private, that allows you to change an implementation of your class without need to change the code where you make use of it. It's better practice to initialize variables via constructors and getters and setters method of the class.

Artem Barger
+3  A: 

If you declare a member inside the class you can set its accessibility e.g

private $varname;
David Archer
NB: This has only been supported since PHP 5 (just in case you're stuck on the 4.x tree), but I'd agree that explicitly setting both variable and method visibility is good practice - just because the PHP interpreter doesn't require it doesn't mean you shouldn't use it.
middaparka
+4  A: 

That variable isn't uninitialized, it's just undeclared.

Declaring variables in a class definition is a point of style for readability. Plus you can set accessibility (private or public).

Anyway, declaring variables explicitly has nothing to do with OOP, it's programming-language-specific. In Java you can't do that because variables must be declared explicitly.

klez
So it's just because PHP is really forgiving that undeclared variables work, right?
Andrew
Right. PHP should emit a warning though... place error_reporting(E_ALL); at the top of your file.
Stephan202
not so much forgiving as being interpreted
David Archer
@klez your 2 sentences about style the accessibility contradict each other :-(
David Archer
*style THEN accessibility
David Archer
Also, declaring variables enables your IDE to provide code completion.
n3rd
In development I use E_ALL, E_STRICT just to get the maximum possible feedback. E_STRICT isn't included in E_ALL (which means "ALL" is probably not quite the right word!!!)
Sohnee
A: 

In general variables should be initialized as soon as you have enough info to do it properly.

If a class variable needs certain info to be sensibly initialized then that info should be passed to the constructor.

Using PHP's syntax to implicitly declare variables at the point of definition is, IMHO a surefire way to introduce bugs - if your class needs a variable then declare it, and use all of the information hiding that OOP affords you.

Visage
+2  A: 

You should always declare your member variables and specify their accessibility within your classes. I like to put this information at the end of the class after my functions.

You should define them as soon as you have enough information to do so. Possibly in the constructor or via setter functions.

It is important to do this because it makes life much easier for people working with your code. They don't have to guess where different properties are coming from or why they're there. Also, most (if not all) IDEs will not pick up on class variables unless you've declared them somewhere. Code completion/hints are one of the many benefits of IDEs and without declaring your variables, you will render that functionality useless.

Mike