note 36593 added to language.oop
| From: | baZz at rn2 dot php dot net | Date: | Wed, 15 Oct 2003 19:02:44 +0000 |
| Subject: | note 36593 added to language.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-58660@lists.php.net to get a copy of this message | ||
If you want to use a class within different pages there is more than one way to do it. The first one
is documented in a note above by russ, the only problem with this method is that you must have
register_globals = ON (in php.ini) and the php recomendation is to set it to off. The other way is
using session array:
<?php /*Page 1*/
session_start();
/*class declaration*/
class Employee{
var $name;
var $lastname;
function newEmpl($name,$lname,$noemp){
$this->name[$noemp] = $name;
$this->lastname[$noemp] = $lname;
}
function getEmpl($noemp){
return $this->name[$noemp];
}
}
$myempl = new Employee;
$myempl->newEmpl('Peter','Gabriel',0);
$myempl->newEmpl('Alex','Bowie',1);
$_SESSION['myempl'] = $myempl; /*Here we add our var to the session array*/
echo "<a href=\"testemp2.php\">go next page to test our class</a>";
?>
<?php/*Page 2*/
session_start();
echo "<p>Testing our class</p><br \>";
class Employee{
var $name;
var $lastname;
function newEmpl($name,$lname,$noemp){
$this->name[$noemp] = $name;
$this->lastname[$noemp] = $lname;
}
function getEmpl($noemp){
return $this->name[$noemp];
}
}
echo $_SESSION['myempl']->getEmpl(0);/*This should print the name of the first employee
that we add in the other page*/
?>
After this you can use the object within the whole session.
Good Luck.
----
Manual Page -- http://www.php.net/manual/en/language.oop.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36593
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36593&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36593&report=yes
Search -- http://master.php.net/manage/user-notes.php