Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How is it possible to use a class variable as a function parameter?
Example:
class Test { protected $img = ''; protected function foo($var = $this->img) { ... some code ... } }
Thus is this good practice?
$var
$this->img
$this
new Test()
protected function foo($var = self::IMG) { ... }
I am not 100% sure, but I thing you can't. You can achieve the same like this:
class Test { protected $img = ''; protected function foo($var = null) { if(is_null($var)) $var = $this->img; ... some code ... } }
Add a comment
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
$varis.. something, if it isn't use$this->img,$thisrefers to the current object instantiated from the class and therefore sort of doesn't exist untilnew Test()has been called; you can use a class constant for default data thoughprotected function foo($var = self::IMG) { ... }