3

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?

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

11 Comments

That second example is about calling a method that uses $this statically, not about member variables.
Look at class B in the second example.
Yes, I did. It's calling A::foo(). There are no member variables there at all.
Class, member variable..all the same to PHP. That's what i was trying to show. -.-
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".
|
1

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';
    }
}

1 Comment

It should be noted that the original example will result in public members, while your example has private members.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.