Объяснение в комментарии
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
}
}
Пример $this
и 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);
, приведенный выше вывод будет таким:
pony
unicorn
I like pony
I like unicorn
object(Test)#49 (3) {
["var"]=>string(4) "pony"
["new_var"]=>string(11) "I like pony"
}
В качестве не e конечный свалка показывает только публичные члены, если бы были частные, это было бы в формате (предположим, private $ foo) ["foo":"Test":private]=>NULL
So $ this-> new_var = $ var; можно получить доступ только изнутри тестового класса, правильно? – Blue
@Blue Нет, он делает общедоступную собственность, общедоступный означает, что к нему можно получить доступ извне класса. – Daan
Хорошо, получилось, спасибо! - Так что с обоими я могу установить свойства для класса извне, не так ли? (public $ var и $ this-> new_var) – Blue