[英]PHP OOP print a variable which has value inside the function but call from outside to be print
Is it possible to print a variable which has the value inside the function but it's called from outside the function to be print in object oriented programming in PHP 是否有可能要打印的所具有的功能中值的变量 ,但它从功能外叫在PHP的面向对象编程进行打印
Let's explain by example 让我们通过例子来解释
My class looks like as: 我的课看起来像:
class my {
public $a;
public function myFunc(){
$name = "fahad";
echo $this->a;
}
}
It should print the value of $name
when the function is call, as I am trying: 我正在尝试调用函数时,它应该打印
$name
的值:
$class = new my();
$class->a = '$name';
$class->myFunc();
But it did't work and print the result as: 但是它不起作用并将结果打印为:
$name
I want it should print the value of variable $name
which is inside the function 我希望它应该打印函数内部的变量
$name
的值
How it can be possible? 怎么可能呢?
Thank You. 谢谢。
You can use variable variables to do this, but it's usually considered bad practice. 您可以使用变量变量来执行此操作,但是通常认为这是不好的做法。
class my {
public $a;
public function myFunc(){
$name = "fahad";
echo ${$this->a};
}
}
$class = new my();
$class->a = 'name';
$class->myFunc();
Output: 输出:
fahad
Inside your function, you can make a check: 在函数内部,您可以进行检查:
public function myFunc(){
if($this->a == '$name'){
$name = 'fahad';
echo $name;
}else echo $this->a;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.