简体   繁体   中英

PHP create an object from a class inside another class

I am trying to create an class object to a class but I keep getting some unknown error.

This is the " helper class " which it takes content from an XML file

<?php

class helperClass {

    private $name;
    private $weight;
    private $category;
    private $location;

    //Creates a new product object with all its attributes
    function __construct(){}

    //List products from thedatabase
    function listProduct(){

        $xmlDoc = new DOMDocument();
        $xmlDoc->load("storage.xml");
        print $xmlDoc->saveXML();
    }
  ?>
}

And here I am trying to create an object from the helperClassclass and call the method listProducts from helperClass , but the code it won't work if I try to instantiate an object of the helperClass

<?php
//Working code...
 class businessLogic {

    private $helper = null;

    public function __construct() {

    }

    public function printXML() {
        $obj = new helperClass();
        $obj->fetchFromXMLDocument();
        // you probably want to store that new object somewhere, maybe:
        $this->helper = $obj;
    }
}
}
?>

After the help of you guys I figured it out and this is what I wanted to do

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        require 'businessLogic.php';
        $obj = new businessLogic();
        $obj->printXML();
        ?>
    </body>
</html>

Your second code snippet is wrong. You can't evaluate code inside a class def. Only inside the methods of a class. Try putting the code in the constructor:

class busniessLogic {
  private $helper = null; // defining the property 'helper' with a literal

  // private $helper = new helperClass(); // this would throw an error because
  // it's not allowed in php.

  public function __construct() {
    $obj = new helperClass();
    $obj->listPruduct();

    // you probably want to store that new object somewhere, maybe:
    $this->helper = $obj;
  }
}

This is just an example of how code can be executed on object creation.

Though I wouldn't actuall use it this way. I'd rather pass the object in or setting it later.

ref : Dependency injection

Once the object is created you can do whatever you want with it. eg call methods (which, of course, have to be defined) on it or pass it to other objects.

Your businessLogic class is not defined correctly.

<?php

include 'helperClass.php';

class busniessLogic {

  function __construct() {
    $obj = new helperClass();
    $obj->listPruduct();
  }
}

$bLogic = new businessLogic();

?>

Whatyou are trying to do is wrong because (taken from the doc)

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated

so you should do something like

class busniessLogic {
   private $obj;

  function __construct(){
    $this->obj = new helperClass();
    $this->obj->listPruduct();
  }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM