1

I'm trying to brush up on some object oriented basics. I wrote this simple script but am confused as to why it works the way it does.

class Foo
{
function run($text = null)
{
    $this->bar = strtolower($text);
    echo $this->bar;    
}
}

$foo = new Foo;
$foo->run('THIS IS SOME TEXT IN LOWER CASE');

The script outputs "this is some text in lower case" as expected.

But what i'm confused about is why I can do this without actually declaring the $bar variable. Why can I just use $this->bar? Maybe i'm not understanding how $this works properly, but I always thought you had to declare a variable prior to using it in the class. For example public $bar;

Thanks for any insights you may have.

4 Answers 4

3

PHP will auto-declare object variables as public if you are accessing them without declaring them as class members before. This is no magic, just a "feature" ;)

However, a good design should not use this "feature" but declare any class members explicitly. Your class should look like this:

class Foo
{
    /**
     * some description
     *
     * @var <type>
     */
    protected $bar;

    ...    

    function run($text = null)
    {
       $this->bar = strtolower($text);
       echo $this->bar;    
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

You asked about the reasons why you should declare a public variable if it will auto declared anyway. There are a few reasons why this makes sense. Once is the the clarity of code, the other that comes in mind are reflectional tasks.
Yes that makes sense, I deleted my prior comment because you basically answered it when you edited your answer and mentioned the "good design" aspect.
As a good advice, I would declare any class member as protected unless there is a proper reason for making it public
The concept of dynamic properties are called Overloading in PHP. Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are processed via magic methods.... And overloading makes sense in some cases, too.
@redreggae Your comment does not fully apply here, as overloading will happen for previously declared protected in the same way as for undeclared members
|
0

You can declare variables in a class (a.k.a. properties) like so:

class Foo {
    public $bar;
}

Note the public keyword. Here, we are declaring the property's visibility. There are three types of visibility: public, private, and protected.

  • public: You can access the property anywhere
  • private: You can only access the property in the class it's declared in
  • protected: You can only access the property in the class it's declared in AND any sub-classes

This also applies to methods. I won't go into too much detail, but you can read more here.


If you don't declare a property, but set one in a method like you did in your example, PHP will automatically declare it for you and assign the value you specified.

Comments

0

You should take a look at http://php.net/manual/en/language.oop5.magic.php

Consider that the same way you declare a variable that did not exist before, out of an object, the same way will happen inside objects, cause they are poorly typed.

Comments

0

You would need to put protected, or private in front of your variable outside of your class methods to declare a class variable as anything but public. In PHP assignment is declaration, if it's not already declared. Note, you can also do this:

$ary = array('a', 'b', 'c', 'd');
foreach($ary as $v){
  $newArray[] = $v;
}

Notice, I never declared $newArray = array(); before the loop.

Comments

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.