Php 仅使用类的名称作为字符串访问类
我试图在一个函数中使用一个已经初始化的类,只将该类的字符串名传递给该函数 例如:Php 仅使用类的名称作为字符串访问类,php,oop,Php,Oop,我试图在一个函数中使用一个已经初始化的类,只将该类的字符串名传递给该函数 例如: class art{ var $moduleInfo = "Hello World"; } $art = new Art; getModuleInfo("art"); function getModuleInfo($moduleName){ //I know I need something to happen right here. I don't want to reinitia
class art{
var $moduleInfo = "Hello World";
}
$art = new Art;
getModuleInfo("art");
function getModuleInfo($moduleName){
//I know I need something to happen right here. I don't want to reinitialize the class since it has already been done. I would just like to make it accessible within this function.
echo $moduleName->moduleInfo;
}
谢谢你的帮助
var$moduleInfo
使$moduleInfo成为一个(php4样式,公共)实例属性,即类art
的每个实例都有自己的(单独的)成员$moduleInfo。因此,该类的名称对您没有多大好处。您必须指定/传递要引用的实例。也许您正在寻找静态属性,请参见
将对象本身作为参数传递到函数中
class art{
var $moduleInfo = "Hello World";
}
function getModuleInfo($moduleName){
//I know I need something to happen right here. I don't want to reinitialize the class since it has already been done. I would just like to make it accessible within this function.
echo $moduleName->moduleInfo;
}
$art = new Art;
getModuleInfo($art);
class art{
var $moduleInfo = "Hello World";
}
function getModuleInfo($moduleName){
//I know I need something to happen right here. I don't want to reinitialize the class since it has already been done. I would just like to make it accessible within this function.
echo $moduleName->moduleInfo;
}
$art = new Art;
getModuleInfo($art);