<?php
class test{
function foo(){
echo $this->abc;
}
}
$test = new test;
$test->abc = 'abc';
?>
Remeber that i don’t declare the variable abc, but i want to set $abc to ‘abc’. How to do this
Sorry because this is a dummy question
Actually, this is already working. But it is a bad practice.
Normally you should do something like this:
Which makes:
Remember to make your class attributes ($abc) always private or protected. It’s encapsulation that makes OOP so powerfull.
If you want to use dynamic variable names, so instead of $this->abc you want to set $this->cba you should use the magic methods __set and __get
More about magic methods can be found here: