简体   繁体   English

如何从一个函数中的一个函数中提取变量到同一类中的另一个函数中

[英]How to extract variable from one function into another in same class in php

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. 我正在使用abstract class通过它我将变量间接声明为global I can not declare variable as global in the class. 我不能在类中将变量声明为global变量。 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? 是否可以在其中回显105作为答案?

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 ? 我的主要动机是我想学习如何在不声明同一类的global的情况下将一个函数的变量值转换为另一个函数,请告诉我这是可能的还是我需要删除我的问题?

<?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): -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();

So, yes, it has to be invoked somewhere. 因此,是的,必须在某个地方调用它。 But depending on what you're trying to achieve there are numerous ways to do so. 但是,根据您要实现的目标,可以采用多种方法。

Each property of the class can be accessed by each method of the same class. 相同类的每个方法都可以访问该类的每个属性。 So you can create methods which are working with the same property. 因此,您可以创建使用相同属性的方法。 And you don't need to create parent abstract class. 而且您不需要创建父抽象类。

class test
{
     protected $te = 5;

     public  function team()
     {         
          $this->te += 100;
     }

     public  function tee()
     {
         return $this->te;
     }

}

$obj = new test();
$obj->team();
echo $obj->tee();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个页面中将变量从一个函数传递给另一个函数? - how to pass variable from one function to another in the same page in php? 无法从一个函数访问同一类中的另一个函数的变量 - Cannot access variable from one function to another function in same class 如何在php中同一类中的另一个函数中调用公共函数中的变量 - how to call a variable in a public function from another function within the same class in php 如何从一个类函数调用变量到另一个类函数 - how to call a variable from one class function to another class function 如何将变量从一个方法传递到同一类中的另一个方法? - How to pass a variable from one method to another in the same class? 如何从另一个函数中的一个函数访问PHP变量 - How to access a PHP variable from one function in another function 从一页读取PHP全局变量,在另一页打印(都在同一PHP类中) - read PHP global variable from one page, print in another (both in same PHP class) 如何将PHP变量从一个函数传递给另一个函数? - How to pass a PHP variable from one function to another? 如何将 PHP 变量从一个函数传递到另一个函数? - How do I pass a PHP variable from one function to another? 将一个 function 中的变量值传递给同一 class 中的另一个 function - To pass value of a variable in one function to another function in same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM