Php 在另一个类中使用一个类变量
如何访问加载到基类中的其他类中的基类变量 基类示例Php 在另一个类中使用一个类变量,php,class,oop,Php,Class,Oop,如何访问加载到基类中的其他类中的基类变量 基类示例 class base { public $get; public function __get( $name ) { require_once $name . '.php'; $this->name = new $name( $this ); return $this->name; } public function __construct( $varfromphpl
class base {
public $get;
public function __get( $name ) {
require_once $name . '.php';
$this->name = new $name( $this );
return $this->name;
}
public function __construct( $varfromphplib ) {
$this->varfromphplib = $varfromphplib;
$this->get = explode( "/", $_SERVER['REQUEST_URI'] );
}
}
$class = new base( $varfromphplib );
另一个类示例(使用_get加载到基类)
我不知道如何在“另一个”类中访问诸如$get之类的基类变量
希望有人能帮我解决这个问题。如果一个类继承了另一个类(A扩展了B),您可以使用$this->variable从父类调用变量。请注意,这仅在继承类没有自己的构造函数并且没有同名变量的情况下才有效 如果其中任何一个阻止您使用$this,那么您可以始终调用父类变量,就像调用无关类中的任何公共变量一样。 i、 e.:A有受保护的变量$A,B扩展了A,但有自己的变量$B。
$objA=新的A();
回声“A->A=”.$objA->A;
当你说“继承的类”时,你知道那是什么意思吗?base
和继承的
之间有什么关系?继承的
扩展了基础
?如果是这样,您的代码是错误的。如果没有,你使用的术语是错误的,这只会妨碍你(和我们)。好吧,我的英语很差,我会尝试编辑以帮助你理解我的情况,但我建议阅读整个章节,在我的“另一”类中调用return$this->get,然后使用print\r($class->another->showGet());没有显示任何内容,$varfromphplib是因为我使用IPBWI,这是我知道如何将IPBWI加载到自己的类中的唯一方法,但这只适用于基类,而不是加载到基类中的类。我不知道怎么说对,让你明白我的问题。
class another {
public function showGet() {
//there I wanna return base class $this->get;
}
public function getLibClass(){
//there I wanna return $varfromphplib variable with baseclass prefix or not if can
}
}
//usage in base class
$this->another->showGet( );