[英]How to pass multiple variables from parent class to child class functions automatically using extract();?
I'm not sure how to code this exactly, but I can surely say it better I believe than my code indicates. 我不确定如何确切地编写代码,但是我可以肯定地说,我相信它比我的代码表明的要好。
I want to extract(); 我想extract(); vars from my parent class and have those vars be automatically available to the functions in the child class. 我的父类中的var,并让这些var可自动用于子类中的函数。
Currently, I have to call the extract(); 当前,我必须调用extract();。 function within each child class function for the vars to be available. 每个子类函数中的var函数都可用。 This is what I'm trying to reduce, the extract(); 这就是我要减少的内容,extract(); call each time to just one time within the child class. 每次在子类中调用一次。
I'm a still new to the __construct(); 我对__construct()还是一个新手。 method as I started out with just calling functions statically. 我刚开始只是静态调用函数的方法。 But I tried to study and make sense of this, but I ONLY find articles online that show how to pass SINGLE vars to other functions from the __construct();. 但是我试图研究并弄清这一点,但是我只能在网上找到文章,展示如何将__varstruct传递给__construct();的其他函数。 I don't find any articles on how to pass multiple vars at once. 我找不到任何有关如何一次传递多个变量的文章。 Specifically using the extract();. 特别是使用extract();。
Can this be done? 能做到吗?
My end goal is simply to reduce the writing of " parent:: " for each var in the child class. 我的最终目标只是减少子类中每个var的“ parent :: ”的编写。 So when needed I can extract the vars and simply write $var
instead of parent::$var
. 因此,在需要时,我可以提取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();
extract is very dodgy when using with OO. 当与OO一起使用时,提取物非常不可靠。 It would be more advisable (and faster execution time wise) to run a foreach and assign $this->$key = $value (since they would be dynamic anyway you could also use magic functions to create getters and setters). 最好运行一个foreach并分配$ this-> $ key = $ value(因为它们无论如何都是动态的,您也可以使用魔术函数来创建getter和setter),这是更可取的选择(并且执行时间更短)。 If you insist on using extract you should have a lok a comment made by " FredLawl " on the same extract() page of php manual. 如果您坚持使用解压缩,则应该在php手册的同一extract()页面上查找“ FredLawl”发表的评论。
Update: 更新:
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;
You can use this base principle to play around and work out a solution that you need. 您可以使用此基本原理来尝试并找到所需的解决方案。
I don't think you're understanding OO very well and are trying to apply a non-OO way of thinking. 我认为您不太了解OO,并尝试采用非OO的思维方式。 An object in PHP (and most other class-based OO languages) is a collection of data and an associated collection of methods that manipulate that data. PHP(以及大多数其他基于类的OO语言)中的对象是数据的集合以及操作该数据的方法的关联集合。 When you create a subclass of a given class, you create a new type of object that extends the collection of variables and the collection of applicable methods. 当创建给定类的子类时,将创建一种新的对象类型,该对象将扩展变量的集合和适用方法的集合。
You also don't quite seem to understand the difference between a class variable (common to all classes) and an instance variable (a variable whose value is unique to a given instance of a class). 您似乎也不太了解类变量(对所有类通用)和实例变量(其值对于给定的类实例是唯一的变量)之间的区别。
For instance variables, any non-private variable is automatically available to the subclass via $this. 对于实例变量,子类可以通过$ 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"
Class variables, aka static variables are common to all instances of a class and are accessed with static:: instead of $this 类变量(又名静态变量)是类的所有实例所共有的,并且使用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.