/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Object passing question

Okay so i’m reading a bunch of articles about trying to pass objects from page to page, which in some cases would help me out a great deal. However, the issue i’m having is that it’s not working for me.

I have created three simple classes to try to get something working so i know the steps to follow however it’s not working.

Is it even possible to do?

Here is the sample code i’m working with :
PHP Class:

[CODE]
<?php
Class testClass{
var $mName;
function testClass(){
}
function getName(){
return $mName;
}
function setName($inName){
$mName = $name;
}
}
[/CODE]

starting HTML page:

[CODE]
<?php
include(‘./testClass.php’);
session_start();
?>
<html>
<head></head>
<body>
<?php
$myNmae = “testing123″;
$myClass = new testClass();
$myClass->setName($myName);
$_SESSION[‘testClass’] = $myClass;
?>
<p><a href=”to.html”>go to the other page</a></p>
</body>
</html>
[/CODE]

The Final page:

[CODE]
<?php
include(‘./testClass.php’);
session_start();
?>
<html>
<head></head>
<body>
<p>
<?php
$testing = $_SESSION[‘testClass’];
$name = $testing->getName();
echo ‘<br /> The name is: ‘. $name . ‘<br />’;
?></p>
</body>
</html>
[/CODE]

The outcome from the to.html page is just “The name is: ” and $name appears to be empty. But no errors are thrown or anything.

Help?

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 25.2008 — Use serialize() and unserialize():
[code=php]
$_SESSION['testClass'] = serialize($myClass);

// . . .

$testing = unserialize($_SESSION['testClass']);
[/code]
Copy linkTweet thisAlerts:
@sbriouxauthorJan 25.2008 — Just tried it.. doesn't seem to work either....
Copy linkTweet thisAlerts:
@jasonahouleJan 25.2008 — You have a typo - $myNmae = "testing123";

Be careful storing objects in sessions. You may see unexpected results. If a value is changed elsewhere it will not be reflected in your instantiated object.
Copy linkTweet thisAlerts:
@sbriouxauthorJan 25.2008 — Sorry I had to type that out by hand instead of copy and pasting from putty. However that error doesn't exist in my code.

But I still does not work.
Copy linkTweet thisAlerts:
@jasonahouleJan 25.2008 — The getName function should return $this->mName. Same goes for the set function $this->mName = $inName
Copy linkTweet thisAlerts:
@jasonahouleJan 25.2008 — Here, try using this to compare and debug.
[code=php]
<?php
session_start();

class TestClass {
private $mName;

public function __construct($name){
$this->mName = $name;
}
public function getName() {
return $this->mName;
}
public function setName($name) {
$this->mName = $name;
}
}

if(!empty($_SESSION['test'])) {
$test = unserialize($_SESSION['test']);
echo "The session value is: " . $test->getName();
}

$test = new TestClass("Jason");
$_SESSION['test'] = serialize($test);
?>
[/code]
Copy linkTweet thisAlerts:
@sbriouxauthorJan 28.2008 — Adding the $this->mName worked!!! Thanks alot!!!
Copy linkTweet thisAlerts:
@jasonahouleJan 28.2008 — Glad to hear it, but do you understand why it worked?
Copy linkTweet thisAlerts:
@sbriouxauthorJan 28.2008 — Basically came down to a scope issue right? I'm still fairly new to PHP.
Copy linkTweet thisAlerts:
@jasonahouleJan 29.2008 — In a way, yes. The $mName variable is a property of the object. So yes, the $mName variable inside the get and set functions was not the same variable as the property of the object. These functions have no knowledge of the object property unless you specifically tell it so. So if you were to access that property directly you would use $myObject->mName.

Hmm, I hope that doesn't confuse you. I don't think I explained that very well. It is the basics of object oriented programming though.
Copy linkTweet thisAlerts:
@sbriouxauthorJan 29.2008 — Makes sense to me. I realized afterwards like I said it was basically a scope issue and i was trying to use a variable that was local to the function but not the object itself.
×

Success!

Help @sbrioux spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...