2

I am in a learning curve with PHP OOP

This is my code

file1.php

class A
{
    public $id;
    public $oop;

    function __construct(){
       $this->time=time();
    }

}
$a = new A();

I tried to access the variable id from a class B which is in another file, code given below.

file2.php

include('file1.php');

class B
{
    function store(){
        global $a; //question updated**
        $x = 1;
        $y = 1;    
    if($x == $y){
        $a->id = $y; // I am setting a value for variable id.
     }
    }
}

$b = new B();

Now I want to access the variable id in my index.php file as below.

index.php

require_once('file2.php');
$b->store(); // Store function executed ** question updated
echo $a->id;

The above code didn't throw me any error, the problem is the value I set for $id isn't echoing.

I also referred some of the answers from here which is nearly similar

call variable from another function in class

how to call a variable from one class function to another class function

But didn't solve my problem.

Please, direct me to achieve my goal here.

Thank you.

10
  • 2
    "The above code didnt throw me any error" - it didn't? Then you should begin by setting up your development environment so that you will see syntax/parse errors and error/warning/notice messages. public id; - that's the first parse error. see also: error_reporting, error_log, display_errors, display_startup_errors at docs.php.net/manual/en/ini.list.php (the last two solely for your development machine). Commented Jan 29, 2016 at 14:29
  • 1
    How does your B object get $a? Commented Jan 29, 2016 at 14:33
  • @VolkerK I am assuming there is no syntax/parse error in the code I have provide and yes I have development environment is enabled to show errors. Commented Jan 29, 2016 at 18:43
  • 1
    "I am assuming there is no syntax/parse error in the code I have provide" - As I've already pointed out public id; is the first syntax error in your code. There's a $ missing in front of id. So, check your setup again. Commented Jan 29, 2016 at 19:43
  • Thanks for showing me that typo. Actually in my script I have the $ for the variables. Now I have corrected in my question. Commented Jan 29, 2016 at 19:48

2 Answers 2

2
<?php

class A
{
    public $foo = 'bar';
}

class B
{
    public function mutateA(A $a)
    {
        $a->foo = 'qux';
    }
}

$a = new A;
echo $a->foo;

$b = new B;
$b->mutateA($a);
echo $a->foo;

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

Comments

0

Please check the following code..

file2.php - I think you forgot to call store method..

class B
{
    function store($a) //note the object passed
    {
        $x = 1;
        $y = 1;    
        if($x == $y)
        {
            $a->id = $y; // I am setting a value for variable id.
        }
    }
}
$b = new B();
$b->store($a); //You forgot to call store method

1 Comment

calling the method is not the solution I require.

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.