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?!
TomTees
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?!
TomTees
Well I did all of that.
Sorry, but it’s pretty hard to trouble-shoot code without looking at the code!
TomTees
They talk a little about it here:
http://ca3.php.net/manual/en/language.oop5.serialization.php
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:
http://ca3.php.net/manual/en/language.oop5.autoload.php
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…
<?php
include ('classes/Soup.class.php');
// Start session AFTER including Classes!!
session_start();
// Put Bowl in Microwave.
$objMicrowave->receiveItem($_SESSION['objBowl']);
TomTees
That’s exactly right!
Good catch!!!
TomTees
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?)
TomTees
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->receiveItem( ) ../cook.php:13
Attached is my code since it is too hard to paste here…
Thanks,
TomTees
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.
TomTees
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);