如何在php中将同一个类中的变量从一个函数提取到另一个函数中(How to extract variable from one function into another in same class

编程入门 行业动态 更新时间:2024-10-26 02:26:59
如何在php中将同一个类中的变量从一个函数提取到另一个函数中(How to extract variable from one function into another in same class in php)

我想将一个函数的变量值用于同一个类的另一个函数。 我正在使用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.

更多推荐

本文发布于:2023-08-07 02:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   变量   函数   类中   一个函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!