Scope of an Object?

What is the scope of an Object?

I created a “Bowl” object on “results.php” and I’m trying to use it in as an argument variable while initializing my “Microwave” object on “cook.php” but that doesn’t seem to be working?!


Well I did all of that.

Sorry, but it’s pretty hard to trouble-shoot code without looking at the code! :frowning:


They talk a little about it here:

But basically, calling session_start will load up $_SESSION with whatever stored information is available. So even though you are not using it until later, objBowl is created immediately.

You can actually write your own session handler and change this behaviour but you might instead want to take a look at autoloading:

Get autoloading working and your class files will be included when and if they are needed without requiring a bunch of include statements.

Why do you have to load the class BEFORE you load the Session??

I didn’t call the Object (in the Session) until well after both lines, so in my mind it shouldn’t matter the order of…

	include ('classes/Soup.class.php');

	// Start session AFTER including Classes!!

	// Put Bowl in Microwave.


That’s exactly right!

Good catch!!!


I’ll pass on opening a zip file. But you don’t really have to do anything to serialize an object into session data.

$_SESSION[‘bowl’] = $bowl;

Later, on another page,
$bowl = $_SESSION[‘bowl’];

You do however need to ensure that the class definition files are included before you try to extract an instance from $_SESSION.

Can you (or someone) please look at my code and help me figure out what is wrong?

Also, what value does serialization provide? (I thought using a SESSION would solve the problem?)


If there is another solution, then no. If there isn’t, then yes.

Serialising is how I do it.

It’s only one line of code to serialise an object and one line of code to unserialise it.

Do I have to serialize it?

Here is the error I get in NetBeans…

Catchable fatal error: Argument 1 passed to Microwave::receiveItem() must be an instance of Bowl, instance of __PHP_Incomplete_Class given, called in /Users/user1/Documents/DEV/++htdocs/Soup/cook.php on line 13 and defined in /Users/user1/Documents/DEV/++htdocs/Soup/classes/Soup.class.php on line 31
Call Stack
#	Time	Memory	Function	Location
1	0.0017	56412	{main}( )	../cook.php:0
2	0.0022	68272	Microwave-&gt;receiveItem( )	../cook.php:13

Attached is my code since it is too hard to paste here…



You need to load the class before you start the session.
So, switch lines 2 and 4 in cook.php and it works (well, at least it does for me).

ok, in that case one option you have is to serialise the class as a session object on page 1 and then unserialise it in page 2.

That’s how I pass my shopping cart objects from 1 page to another.

No, I create an object on page 1 and I need to pass thats ame object to page 2.

You are instantiating things on the same page.


Class definitions are global. Just need to be sure to include the class definition file first. Or maybe I don’t understand your question.

require_once 'Bowl.php';
require_once 'Microwave.php';

$bowl = new Bowl();
$microwave = new Microwave($bowl);