我想将一个函数的变量值用于同一个类的另一个函数。 我正在使用abstract class ,我使用它来间接地将变量声明为global变量。 我不能在类中将变量声明为global变量。 我的演示代码如下:
<?php abstract class abc { protected $te; } class test extends abc { public function team() { $te = 5; $this->te += 100; } public function tee() { $tee = 51; return $this->te; } } $obj = new test(); echo $obj->tee(); //echo test::tee(); ?>这可能是我可以回答105作为答案吗?
我的主要动机是我想学习如何从一个函数到另一个函数获取变量值,而不是在同一个类中声明全局请告诉我这是否可能或我需要删除我的问题?
I want to use variable value from one function into another function of same class. I am using abstract class using which I am declaring variable as global indirectly. I can not declare variable as global in the class. My demo code is as follows:
<?php abstract class abc { protected $te; } class test extends abc { public function team() { $te = 5; $this->te += 100; } public function tee() { $tee = 51; return $this->te; } } $obj = new test(); echo $obj->tee(); //echo test::tee(); ?>Is this possible that I can echo 105 as answer there?
My main motive is I want to learn that how to get variable value from one function into another using without declaring that global in the same class Please let me know Is this possible OR I need to delete my question ?
最满意答案
<?php abstract class abc { protected $te; } class test extends abc { public function __construct() { $this->te = 5; } public function team() { $this->te += 100; } public function tee() { return $this->te; } } $obj = new test(); $obj->team(); echo $obj->tee();- 编辑:至少使用抽象“功能”:
<?php abstract class abc { protected $te; abstract public function team(); public function tee() { return $this->te; } } class test extends abc { public function __construct() { $this->te = 5; } public function team() { $this->te += 100; } } $obj = new test(); $obj->team(); echo $obj->tee();- edi2:既然您已经询问是否必须调用团队(然后删除该评论):
<?php abstract class abc { protected $te; abstract public function team(); public function tee() { $this->team(); return $this->te; } } class test extends abc { public function __construct() { $this->te = 5; } public function team() { $this->te += 100; } } $obj = new test(); echo $obj->tee();所以,是的,它必须在某处调用。 但是,根据你想要实现的目标,有很多方法可以实现。
<?php abstract class abc { protected $te; } class test extends abc { public function __construct() { $this->te = 5; } public function team() { $this->te += 100; } public function tee() { return $this->te; } } $obj = new test(); $obj->team(); echo $obj->tee();-- edit: to make at least some use of the abstract "feature":
<?php abstract class abc { protected $te; abstract public function team(); public function tee() { return $this->te; } } class test extends abc { public function __construct() { $this->te = 5; } public function team() { $this->te += 100; } } $obj = new test(); $obj->team(); echo $obj->tee();-- edi2: since you've asked whether you must invoke team (and then deleted that comment):
<?php abstract class abc { protected $te; abstract public function team(); public function tee() { $this->team(); return $this->te; } } class test extends abc { public function __construct() { $this->te = 5; } public function team() { $this->te += 100; } } $obj = new test(); echo $obj->tee();So, yes, it has to be invoked somewhere. But depending on what you're trying to achieve there are numerous ways to do so.
更多推荐
发布评论