views:

50

answers:

3

Hi,

I've been looking at some code and am having a hard time working out variable declaration in php classes. Specifically it appears that the code i'm looking at doesn't declare the class variables before it uses them. Now this may be expected but I can't find any info that states that it is possible. So would you expect this:

class Example
{

    public function __construct()
    {
        $this->data = array();
        $this->var = 'something';

    }

}

to work? and does this create these variables on the class instance to be used hereafter?

+1  A: 

So would you expect this: (code sample) to work?
Yes. It's pretty bad practice (at least it makes my C++ skin crawl), but it wouldn't surprise me in the slightest. See example 2 in the following page for an example of using another class without declaring it beforehand. http://www.php.net/manual/en/language.oop5.basic.php It will throw an error if E_STRICT is enabled.

And does this create these variables on the class instance to be used hereafter?

Yep. Ain't PHP Fun? Coming from a C++/C# background, PHP took a while to grow on me with its very loose typing, but it has its advantages.

Caladain
That second example is about calling a method that uses `$this` statically, not about member variables.
deceze
Look at class B in the second example.
Caladain
Yes, I did. It's calling `A::foo()`. There are no member variables there at all.
deceze
Class, member variable..all the same to PHP. That's what i was trying to show. -.-
Caladain
I think you're misunderstanding the example. A class is automatically available in any scope, no surprises there. And the class *is* "declared beforehand", otherwise you couldn't use it. The example is simply calling a class method statically. There's nothing that needs to be declared to do so, it's like calling any other method. The point is to demonstrate how the special variable `$this` behaves in a method when called statically, as part of an object, and statically from another object. The `E_STRICT` is generated by the use of `$this` in `A::foo()`, not because a "class was used".
deceze
~facepalm~ Yes, that's what example 2 is trying to say (in the context of the example). Showing off the $this variable. I was trying to say, it *also* shows off that you don't need to delcare a private variable within Class B of Class A, like you would in C++. I was hijacking the example to show a feature in the context of this question. Probably should have rolled my own example.
Caladain
Yeah, without context of how this needs to be done in C++ it's a bad example. :P I don't know too much about C++, but can you not statically call class methods of another class/object in C++?
deceze
You'd need to declare within Class B a private instance of Class A. Then you'd need to do one of three things: 1.) Get passed an existing instance of Class A in the constructor. 2.) Have a setter that sets Class A. This would be called by someone else. 3.) Instantiate your own copy of Class A in the constructor.
Caladain
Are you sure? You're talking about instantiating an object... Calling it **statically** though seems to work the same way as it does in PHP (and as in the example): http://www.cppreference.com/wiki/keywords/static
deceze
The variable numFoos is declared private in the class. You can declare class members/variables in any order..it's generally good practice in C++ to put all your variable declarations at the top after constructor/destructor, but before anything else.If you're looking at something specific, a bit more detail as to specific sections would be nice :-)
Caladain
I think we're talking about different things... Suffice it to say that the example you quoted is not related to this question. =P
deceze
+1  A: 

This works the same as a normal variable declaration would work:

$foo = 'bar'; // Created a new variable

class Foo {
    function __construct() {
        $this->foo = 'bar'; // Created a new variable
    }
}

PHP classes are not quite the same as in other languages, where member variables need to be specified as part of the class declaration. PHP class members can be created at any time.

Having said that, you should declare the variable like public $foo = null; in the class declaration, if it's supposed to be a permanent member of the class, to clearly express the intent.

deceze
+1  A: 

That's completely functional, though opinions will differ. Since the creation of the class member variables are in the constructor, they will exist in every instance of the object unless deleted.

It's conventional to declare class member variables with informative comments:

class Example
{
    private $data;  // array of example data

    private $var;   // main state variable

    public function __construct()
    {
        $this->data = array();
        $this->var = 'something';
    }
}
wallyk