我不确定如何准确地对其进行编码,但我可以肯定地说,我相信它比我的代码显示的要好。
我想提取();来自父类的变量,并使这些变量自动可用于子类中的函数。
目前,我必须调用extract();vars的每个子类函数中的函数。这就是我试图减少的extract();每次只调用子类中的一次。
我对__construct()还是个新手;方法,因为我刚开始只是静态调用函数。但我试图研究并理解这一点,但我只在网上找到了一些文章,展示了如何将SINGLE变量传递给__construct();中的其他函数;。我找不到任何关于如何同时传递多个变量的文章。特别是使用extract();。
这能做到吗?
我的最终目标只是减少为子类中的每个var写入">parent::"。因此,当需要时,我可以提取变量并简单地写入$var
而不是parent::$var
。
// ----------------------------------------------------------------------------------------------------
// Concept One
// ----------------------------------------------------------------------------------------------------
class Parent_Vars {
public static function get_vars() {
$vars = array(
'var_1' => 'var_1',
'var_2' => 'var_2',
'var_3' => 'var_3',
);
return $vars;
}
}
class Parent_Vars extends class Child_Vars {
public static $instance;
static function getInstance() {
if (!self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
public function __construct() {
parent::get_vars();
}
// This method DOES NOT work
public static function echo_var_method_1() {
//extract(parent::get_vars()); If I uncomment this, my vars below will work
// But I don't want to call extract(parent::get_vars()); for every function I need.
// I would like the vars to already be available from the __construct();
echo $var_1; // returns error = undefined var
echo $var_2; // returns error = undefined var
echo $var_3; // returns error = undefined var
echo parent::$var_1; // Works, but I'm trying to reduce writing parent:: everytime
echo parent::$var_2; // Works, but I'm trying to reduce writing parent:: everytime
echo parent::$var_3; // Works, but I'm trying to reduce writing parent:: everytime
}
// This method DOES work
public static function echo_var_method_2() {
extract(parent::get_vars()); // I'm trying NOT to call the var extract for each function, but for the whole class at once
echo $var_1; // echoes "var_1" !! No need to write parent:: everytime for the vars
echo $var_2; // echoes "var_2" !! No need to write parent:: everytime for the vars
echo $var_3; // echoes "var_3" !! No need to write parent:: everytime for the vars
}
}
$object = new Child_Vars();
Child_Vars::echo_var_method_1();
Child_Vars::echo_var_method_2();
// ----------------------------------------------------------------------------------------------------
// Concept Two - just slightly different with the parent class having its own __construct(); and the child __construct(); calling the parent __construct();
// ----------------------------------------------------------------------------------------------------
class Parent_Vars {
public function __construct() {
extract(self::get_vars());
}
public static function get_vars() {
$vars = array(
'var_1' => 'var_1',
'var_2' => 'var_2',
'var_3' => 'var_3',
);
return $vars;
}
}
class Parent_Vars extends class Child_Vars {
public static $instance;
static function getInstance() {
if (!self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
public function __construct() {
parent::__construct();
}
// This method DOES NOT work
public static function echo_var_method_1() {
//extract(parent::get_vars()); If I uncomment this, my vars below will work
// But I don't want to call extract(parent::get_vars()); for every function I need.
// I would like the vars to already be available from the __construct();
echo $var_1; // returns error = undefined var
echo $var_2; // returns error = undefined var
echo $var_3; // returns error = undefined var
echo parent::$var_1; // Works, but I'm trying to reduce writing parent:: everytime
echo parent::$var_2; // Works, but I'm trying to reduce writing parent:: everytime
echo parent::$var_3; // Works, but I'm trying to reduce writing parent:: everytime
}
// This method DOES work
public static function echo_var_method_2() {
extract(parent::get_vars()); // I'm trying NOT to call the var extract for each function, but for the whole class at once
echo $var_1; // echoes "var_1" !! No need to write parent:: everytime for the vars
echo $var_2; // echoes "var_2" !! No need to write parent:: everytime for the vars
echo $var_3; // echoes "var_3" !! No need to write parent:: everytime for the vars
}
}
$object = new Child_Vars();
Child_Vars::echo_var_method_1();
Child_Vars::echo_var_method_2();
与OO一起使用时,提取非常不可靠。运行foreach并分配$this->$key=$value(因为它们无论如何都是动态的,所以您也可以使用魔术函数来创建getter和setter)会更明智(而且执行时间更快)。如果你坚持使用extract,你应该在php手册的同一extract()页面上有一个由"FredLawl"发表的lok-a评论。
更新:
class MyClass
{
public function __construct($data)
{
foreach($data as $key => $value){
$this->$key = $value
}
}
}
class MyClass2 extends MyClass
{
public function __get($name) {
return $this->$name;
}
}
$instance = new MyClass2($variableArray);
$xtractedVar = $instance->variableKey;
你可以利用这一基本原则来解决问题,并制定出你需要的解决方案。
我认为您对OO的理解不是很好,并且正在尝试应用非OO的思维方式。PHP(以及大多数其他基于类的OO语言)中的对象是数据的集合和操作这些数据的相关方法的集合。当您创建给定类的子类时,您将创建一种新类型的对象,该对象扩展变量的集合和适用方法的集合。
您似乎也不太理解类变量(所有类通用)和实例变量(其值对类的给定实例是唯一的变量)之间的区别。
例如变量,任何非私有变量都可以通过$this自动提供给子类。
class SuperClass {
protected $var = "";
public function showVar ()
{
echo $this -> var . PHP_EOL;
}
public function setVal ($newVal)
{
$this -> var = $newVal;
}
}
class SubClass extends SuperClass {
public function showVarInString ()
{
echo "Hello, I'm a subclass with value " . $this -> var;
}
}
$a = new SuperClass ();
$b = new SubClass ();
$a -> setVal (1);
$b -> setVal (4);
$a -> showVar (); // "1"
$b -> showVar (); // "4"
$b -> showVarInString (); // "Hello, I'm a subclass with value 4"
类变量,也称为静态变量,对类的所有实例都是通用的,并且使用static::而不是$this 访问
class SuperClass
{
protected static $var = 1234;
}
class SubClass extends SuperClass
{
public function showVar ()
{
echo static::$var . PHP_EOL;
}
}