Host to access a variable in static method Hello guys,
if I have something like this
class test
{
public function __construct()
{
$var = 123;
}
public static method mytest()
{
// How do I access the $var from the construct?
}
}
I'm trying to figure out how to share the variables in the same class among methods. I have the hardest problem understanding this concept.
Thanks!
Unsure why you would (use dynamic methods instead), but try this :)
class test
{
protected $var;
public function __construct()
{
$this->var = 123;
}
public static method mytest()
{
$self = new static; //OBJECT INSTANTIATION
$var = $self->var;
// How do I access the $var from the construct?
}
}
how does this relate to your original question?
Actually, its related due to this is my actual code. I'm not sure if due to the Variable that I'm trying to pass inside the method.
I did change it from the static method.
Just using a simple keyword to access protected/private variable from a static method.
class test
{
public function __construct()
{
$var = 123;
}
public static method mytest()
{
//Easy way
self::$var
}
}
@hasan_dev Why have you signed up just to reply to a thread that’s almost half a decade old? 🤷♂️
Please sign in or create an account to participate in this conversation.