0

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 ?

2 Answers 2

4
<?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.

Sign up to request clarification or add additional context in comments.

3 Comments

Brother, can you please update your second last program with corresponding comments? So that I can understand the flow of program.
As function team() is returning nothing when I echo $obj->team(); it echo nothing. So my question is as team() is returning nothing then how tee gets updated with updated value?
team() itself alters the member variable te of the instance/object. see en.wikipedia.org/wiki/Member_variable
0

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

2 Comments

you are declaring $te = 5; as global I do not want to declare global variables directly in the program.
$te is global within the class. Any property of the class is accessible by its methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.