0

I am misunderstanding how php object references are meant to work. I understand that in php 5 reference assignment is automatic for objects. I understand how reference assignment works for non objects.

I want to pass a reference to an object instance from a method to the calling code and then call a method of that class using the instance reference.

When I attempt this I get :

Fatal error: Call to a member function update_Sessions_For_Schedule() on a non-object in...

I have read through a number of similar SO postings but I am no closer finding why I am unable to do this.

So what I have is:

class Schedule checks with class Time_Tabler that a new schedule can be added to the time table prior to allowing it's creation. So obviously during this process Time_Tabler is instantiated.

I want to access this instance from the php code which instantiated the schedule and then call method update_Sessions_For_Schedule() using that instance.

So I have method get_Checked_Time_Table() in class Schedule :-

I have tried this in two formats:

With explicit reference assignment:

public function &get_Checked_Time_Table(){
    global $test_Time_Tabler;
    return $test_Time_Tabler;
}

where $test_Time_Tabler is instantiated in an earlier class Schedule method.

which is called by

    $tested_Time_Table =& $complete_Schedule->get_Checked_Time_Table();

Without explicit reference assignment:

public function get_Checked_Time_Table(){
    global $test_Time_Tabler;
    return $test_Time_Tabler;
}

where $test_Time_Tabler is instantiated in an earlier class Schedule method.

which is called by

$tested_Time_Table = $complete_Schedule->get_Checked_Time_Table();

In either case

$tested_Time_Table->update_Sessions_For_Schedule($update_As_Insert);

produces the Fatal error.

I do not understand why the reference is not recognised as being an object of class Time_Tabler thus making it possible to call the method.

Any thoughts welcomed.

ZeissS
  • 11,867
  • 4
  • 35
  • 50
codepuppy
  • 1,130
  • 2
  • 15
  • 25
  • @JohnB I know that the reference assignment is no longer required by php 5 but I understood that it could be referenced this way if required. Hence I tried both ways when it didn't work. Are you saying ... well I don't know what are you saying. – codepuppy Oct 03 '12 at 18:44
  • 1
    don't pass references to objects just pass the objects themselves. If you go around passing references it just gonna make your server error log grow exponentially. – Rooster Oct 03 '12 at 19:55
  • @JohnB I assume that you are suggesting that I will get a server error if explicitly use reference assignment. I think I may have misled you slightly it was never my intention to employ explicit reference assignment I was only using this as a belt and brace to see if I could find establish what was happening to the reference. – codepuppy Oct 04 '12 at 10:37

1 Answers1

0

I have resolved this problem.

The resolution to the problem was, as always stupidly simple. The instance was not persisting so by the time I wanted to refer back to it it didn't exist.

So to ensure that the instance still exists at the point of refer back I made a clone of it.

codepuppy
  • 1,130
  • 2
  • 15
  • 25