Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅使用类的名称作为字符串访问类_Php_Oop - Fatal编程技术网

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);