PHP crate an object from a class inside another class -


hello trying create class object class keep getting unknown error don't seem resolve

this "helper class" takes content xml file

class helperclass {      private $name;     private $weight;     private $category;     private $location;      //creates new puduct object it's atributes     function __construct(){}      //list pruducts thedatabase     function listpruduct(){          $xmldoc = new domdocument();         $xmldoc->load("storage.xml");         print $xmldoc->savexml();     }    ?>    } 

and here trying crate object hleprclassclass , call method listproducts helperclass, code wont work if try instantiate object of helperclass

<?php //working code...  class busniesslogic {  private $helper = null;  public function __construct() {  }  public function printxml() {     $obj = new helperclass();     $obj->fetchfromxmldocument();     // want store new object somewhere, maybe:     $this->helper = $obj; } 

}

}  ?> 

after of guys figured out , wanted do

<!doctype html> <html>     <head>         <meta http-equiv="content-type" content="text/html; charset=utf-8">         <title></title>     </head>     <body>         <?php         require 'bussineslogic.php';         $obj = new busniesslogic();         $obj->printxml();         ?>     </body> </html> 

your second code snippet wrong. can't evaluate code inside class def. inside methods of class. try putting code in constructor:

class busniesslogic {   private $helper = null; // defining property 'helper' literal    // private $helper = new helperclass(); // throw error because   // it's not allowed in php.    public function __construct() {     $obj = new helperclass();     $obj->listpruduct();      // want store new object somewhere, maybe:     $this->helper = $obj;   } } 

this example of how code can executed on object creation.

though wouldn't actuall use way. i'd rather pass object in or setting later.

ref: dependency injection

once object created can whatever want it. e.g. call methods (which, of course, have defined) on or pass other objects.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -