Blue
Blue

Reputation: 23

What is the difference between these PHP variables

Can anyone tell me what is the differnce between:

class Test {
  public $var; // I know this can be accessed from outside...

  public function __construct($var) {
    $this->var = $var; // This 
    $this->new_var = $var; // And this ... ? this is only internal like I would write private $new_var; ?
  }

  public function echoVar() {
    echo $this->new_var;
  }
}

Upvotes: 0

Views: 72

Answers (3)

nerdlyist
nerdlyist

Reputation: 2857

Explanation in comments

class Test {
    public $var; // This is a member variable or attribute.
                 // Something that this class has access to 
                 // and any of its sub-children

    public function __construct($var) {
        $this->var = $var; //If you mean $this then it is
                           // the current instance of this class see below
                           //If you mean $var it is a the parameter that 
                           //is passed to the method. See below as well

        $this->new_var = $var; // update: this adds a public member to the object
                               // essentially another $var just not easily known.
                               // Not sure a good use for this except confusion and chaos.
                               // If it were just $new_var then it is a 
                               // scoped variable in this method
    }
}

Example of $this and parameters

$testPony = new Test("pony"); //An instance of test with a parameter of pony
$testUnicorn = new Test("unicorn"); //An instance of test with a parameter of unicorn

echo $testPony->var . "<br>";
echo $testUnicorn->var . "<br>";

echo $testPony->new_var . "<br>";
echo $testUnicorn->new_var . "<br>";

echo "<pre>";
var_dump($testPony);

The above would output this:

pony
unicorn
I like pony
I like unicorn
object(Test)#49 (3) {
   ["var"]=>string(4) "pony"
   ["new_var"]=>string(11) "I like pony"
}

As a note the end dump only show public members if there were private ones it would be in the format (assume private $foo) ["foo":"Test":private]=>NULL

Upvotes: 0

iainn
iainn

Reputation: 17433

There actually won't be any fundamental difference between those two - if you write to an undeclared property in PHP (from either inside or outside the class), it will dynamically create a new public property. So given the following script:

<?php
class Test {
  public function __construct() {
    $this->foo = 'foo';
  }
}

$test = new Test;
echo $test->foo;

you'll get the output foo. See https://eval.in/632326.

In short, properties need to be explicitly declared as private or protected if you want to hide them.

You can also implement the magic methods __get and __set on your class in order to better deal with calls to read or write dynamic properties. See the manual page on overloading for more information.

Upvotes: 2

Daan
Daan

Reputation: 12246

I'll add comment lines to specifiy each variable.

class Test {
  public $var; // I know this can be accessed from outside...
  //Variable/property that can be accessed from outside the class like you mentioned.    

  public function __construct($var) {
    $this->var = $var; // This 
    //$this calls a non static method or property from a class, in this case the property `public $var`       

    $this->new_var = $var; // And this ... ? this is only internal like I would write private $new_var; ?
    //Creates a new public property in the Test class 
  }

  public function echoVar() {
    echo $this->new_var;
    //echo the property new_var from Test class.
  }
}

Upvotes: 0

Related Questions