[英]How to pass multiple variables from parent class to child class functions automatically using extract();?
我不确定如何确切地编写代码,但是我可以肯定地说,我相信它比我的代码表明的要好。
我想extract(); 我的父类中的var,并让这些var可自动用于子类中的函数。
当前,我必须调用extract();。 每个子类函数中的var函数都可用。 这就是我要减少的内容,extract(); 每次在子类中调用一次。
我对__construct()还是一个新手。 我刚开始只是静态调用函数的方法。 但是我试图研究并弄清这一点,但是我只能在网上找到文章,展示如何将__varstruct传递给__construct();的其他函数。 我找不到任何有关如何一次传递多个变量的文章。 特别是使用extract();。
能做到吗?
我的最终目标只是减少子类中每个var的“ parent :: ”的编写。 因此,在需要时,我可以提取vars并直接编写$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),这是更可取的选择(并且执行时间更短)。 如果您坚持使用解压缩,则应该在php手册的同一extract()页面上查找“ FredLawl”发表的评论。
更新:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.