home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Neil's C++ Stuff
/
2015-03-neilstuff.zip
/
oldblogfiles
/
2004-11-12-ex-references.php.txt
< prev
next >
Wrap
Text File
|
2015-03-14
|
2KB
|
96 lines
<?php
//*[ex-references.php]********************************************************
//
// Example on Classes and References
//
// This is tested on PHP 4.1.2 which is very particular about how references
// are used. If you follow these considerations, your scripts will work in
// PHP 4.1.2 and PHP 5. PHP 3 compatibility is NOT guaranteed whatsoever.
//
// Caveats:
//
// ---> Output parameters CANNOT pass references; thus to get a reference
// from ANY function, it must be returned.
//
// ---> When an object is COPIED, the default constructor is NOT called. All
// member data is simply copied verbatum.
//
// ---> In order for an object's reference to be placed into a variable from
// the return of a function; 1.) the function name must be preceded by
// an ampersand and 2.) the receiving variable must "assign by reference"
// like so:
//
// $o =& foo();
//
// ---> When using array_push() to push a REFERENCE, the variable must be
// explicitly preceded by an ampersand ... otherwise a copy is pushed.
//
// ---> If a function explicitly accepts a reference parameter, then making
// calls to it do NOT require a preceding ampersand; see use of
// Object::AddChild() below.
//
//-[History]------------------------------------------------------------------
// 2004-09-21 by NeilO .... Created.
//
//****************************************************************************
global $ObjectNo;
$ObjectNo = 0;
class Object
{
var $Parent;
var $Child;
var $Data;
function Object()
{
$GLOBALS['ObjectNo']++;
echo("Creating Object #" . $GLOBALS['ObjectNo'] . "<BR/>\r\n");
$this->Child = array();
$this->Data = "Wrongt";
}
function &CreateChild()
{
$o = new Object;
$o->Data = "Not Referenced";
$this->AddChild($o);
return $o;
}
function AddChild(&$o)
{
$this->Parent =& $o;
array_push($this->Child, &$o);
}
function GetChild($i, &$o)
{
$o = $this->Child[$i];
}
function &GetNull()
{
return null;
}
};
$parent = new Object();
$parent->Data = "Correct";
$child1 =& $parent->CreateChild();
$child1->Data = "Referenced";
echo("CHILD 1 DATA: " . $parent->Child[0]->Data . "; PARENT = " . $parent->Data . "<BR/>\r\n");
$parent->CreateChild();
$parent->GetChild(1, &$child2);
$child2->Data = "Referenced";
echo("CHILD 2 DATA: " . $parent->Child[1]->Data . "; PARENT = " . $parent->Data . "<BR/>\r\n");
$child3 = $parent->GetNull();
if (isset($child3))
echo("CHILD 3 IS NOT NULL");
else
echo("CHILD 3 IS NULL");
?>