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 >
Text File  |  2015-03-14  |  2KB  |  96 lines

  1. <?php
  2. //*[ex-references.php]********************************************************
  3. //
  4. // Example on Classes and References
  5. //
  6. // This is tested on PHP 4.1.2 which is very particular about how references
  7. // are used.  If you follow these considerations, your scripts will work in
  8. // PHP 4.1.2 and PHP 5.  PHP 3 compatibility is NOT guaranteed whatsoever.
  9. //
  10. // Caveats:
  11. //
  12. // ---> Output parameters CANNOT pass references; thus to get a reference
  13. //        from ANY function, it must be returned.
  14. //
  15. // ---> When an object is COPIED, the default constructor is NOT called.  All
  16. //        member data is simply copied verbatum.
  17. //
  18. // ---> In order for an object's reference to be placed into a variable from
  19. //        the return of a function; 1.) the function name must be preceded by
  20. //        an ampersand and 2.) the receiving variable must "assign by reference"
  21. //        like so:
  22. //
  23. //        $o =& foo();
  24. //
  25. // ---> When using array_push() to push a REFERENCE, the variable must be
  26. //        explicitly preceded by an ampersand ... otherwise a copy is pushed.
  27. //
  28. // ---> If a function explicitly accepts a reference parameter, then making
  29. //        calls to it do NOT require a preceding ampersand; see use of
  30. //        Object::AddChild() below.
  31. //
  32. //-[History]------------------------------------------------------------------
  33. // 2004-09-21 by NeilO .... Created.
  34. //
  35. //****************************************************************************
  36. global $ObjectNo;
  37. $ObjectNo = 0;
  38. class Object
  39. {
  40.     var $Parent;
  41.     var $Child;
  42.     var $Data;
  43.     
  44.     function Object()
  45.     {
  46.         $GLOBALS['ObjectNo']++;
  47.         echo("Creating Object #" . $GLOBALS['ObjectNo'] . "<BR/>\r\n");
  48.         $this->Child = array();
  49.         $this->Data = "Wrongt";
  50.     }
  51.     
  52.     function &CreateChild()
  53.     {
  54.         $o = new Object;
  55.         $o->Data = "Not Referenced";
  56.         $this->AddChild($o);
  57.         return $o;
  58.     }
  59.     
  60.     function AddChild(&$o)
  61.     {
  62.         $this->Parent =& $o;
  63.         array_push($this->Child, &$o);
  64.     }
  65.     
  66.     function GetChild($i, &$o)
  67.     {
  68.         $o = $this->Child[$i];
  69.     }
  70.     
  71.     function &GetNull()
  72.     {
  73.         return null;
  74.     }
  75. };
  76.  
  77. $parent = new Object();
  78. $parent->Data = "Correct";
  79.  
  80. $child1 =& $parent->CreateChild();
  81. $child1->Data = "Referenced";
  82. echo("CHILD 1 DATA: " . $parent->Child[0]->Data . "; PARENT = " . $parent->Data . "<BR/>\r\n");
  83.  
  84. $parent->CreateChild();
  85. $parent->GetChild(1, &$child2);
  86. $child2->Data = "Referenced";
  87. echo("CHILD 2 DATA: " . $parent->Child[1]->Data . "; PARENT = " . $parent->Data . "<BR/>\r\n");
  88.  
  89. $child3 = $parent->GetNull();
  90.  
  91. if (isset($child3))
  92.     echo("CHILD 3 IS NOT NULL");
  93. else
  94.     echo("CHILD 3 IS NULL");
  95.  
  96. ?>