PHP OOP 打印一个在函数内部具有值但从外部调用以进行打印的变量 [英] PHP OOP print a variable which has value inside the function but call from outside to be print
本文介绍了PHP OOP 打印一个在函数内部具有值但从外部调用以进行打印的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以打印一个变量,它的值在函数内部,但它是从函数外部调用的,以便在对象中打印面向 PHP 的编程
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
举例说明
我的类看起来像:
class my {
public $a;
public function myFunc(){
$name = "fahad";
echo $this->a;
}
}
它应该在调用函数时打印 $name
的值,正如我正在尝试的那样:
It should print the value of $name
when the function is call, as I am trying:
$class = new my();
$class->a = '$name';
$class->myFunc();
但它没有工作并将结果打印为:
But it did't work and print the result as:
$name
我希望它应该打印变量 $name
的值,它是内部函数
I want it should print the value of variable $name
which is inside the function
怎么可能?
谢谢.
推荐答案
您可以使用 variable variables 这样做,但通常被认为是不好的做法.
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();
输出:
fahad
这篇关于PHP OOP 打印一个在函数内部具有值但从外部调用以进行打印的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文