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.

Community
  • 1
  • 1
codejc
  • 41
  • 6
  • 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 http://docs.php.net/manual/en/ini.list.php (the last two solely for your development machine). – VolkerK Jan 29 '16 at 14:29
  • 1
    How does your B object get $a? – Progrock Jan 29 '16 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. – codejc Jan 29 '16 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. – VolkerK Jan 29 '16 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. – codejc Jan 29 '16 at 19:48
  • And now, [when calling `$b->store()`](https://3v4l.org/k7IPq), you should get the message `Warning: Creating default object from empty value` – VolkerK Jan 29 '16 at 19:54
  • @RyanVincent, Well I am executing $b-> store() in a process php file and I have updated my question with global variable although I have tried this and didn't work for me. – codejc Jan 29 '16 at 20:30
  • @RyanVincent, I have updated the question. – codejc Jan 29 '16 at 20:38
  • @RyanVincent, Yes, my development environment error reporting is set to 'E_ALL'. – codejc Jan 29 '16 at 21:02
  • setting error reporting to E_ALL is not enough: you have to actually tell the environment to *display* them as well `ini_set('display_errors',1);` at the top of the script – Félix Adriyel Gagnon-Grenier Feb 01 '16 at 01:46

2 Answers2

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
Progrock
  • 7,373
  • 1
  • 19
  • 25
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
Kamal Joshi
  • 1,298
  • 3
  • 25
  • 45