【发布时间】:2017-01-10 17:18:43
【问题描述】:
我不确定如何准确编码,但我肯定可以说得比我的代码显示的更好。
我想 extract(); 父类中的 vars 并让这些 vars 自动可用于子类中的函数。
目前,我必须调用 extract();每个子类函数中的函数以使变量可用。这就是我想要减少的,extract();每次只在子班内调用一次。
我还是 __construct(); 的新手。方法,因为我刚开始只是静态调用函数。但是我试图研究并理解这一点,但我只在网上找到了展示如何将 SINGLE vars 从 __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 实践(事实上,随着 static 关键字出现在任何地方,它可能根本不是 OO!静态方法是试图将非 OO 思维应用于 OO 模型的代码味道)。如果您在超类中声明一个变量,那么它在子类中自动可用(除非您将其声明为私有)。没有必要缺少提取物,绝对不推荐。
-
我应该补充一点,我不想让这些全局变量随处可用,而只是在需要时可供子类及其函数使用。我希望这是有道理的。我不确定 PHP 是如何收集变量的,但根据我的理解,每次调用 extract(); php 将提取所有这些变量。当提取中只能使用几个变量时,我不想这样做。所以我想尽可能减少调用提取,这就是为什么我想在需要时为整个班级调用一次。希望这是有道理的。 - 哦好的。刚看到你的回复。 @戈登M
-
对于 OO 静态关键字它是。我明白,正如我所说,我开始只静态调用函数,因为我还没有完全理解 obj 逻辑。但我知道构造可以在实例创建时自动使某些东西可用,所以这是我尝试构造的逻辑。但最终我只希望我提取的变量可用于我的子类函数。
-
为什么不创建一个
__get方法来查找数组,而不是将它们提取到变量中? -
@Barmar 很好,我可以打电话给
parent::,这就是我已经在做的事情。我所有的变量都已经作为 public static $var;但我试图找到一种方法将所有这些变量拉到另一个类中,而不必编写 parent::$var。只是因为我注意到 parent::$var 变得非常重复。这可能不是什么大问题,但我有这个想法并想深入研究它,特别是因为我没有找到关于这个概念的文章,但也许这就是为什么,哈哈。
标签: php class oop variables global-variables