简体   繁体   English

PHP从另一个类中的一个类创建对象

[英]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 这是“ helper class ”,它从XML文件中获取内容

<?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 在这里,我想创建从一个对象helperClassclass并调用该方法listProductshelperClass ,但代码如果我尝试实例化的对象,它不会工作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. 您不能在类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 ref依赖注入

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. 您的businessLogic类未正确定义。

<?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. 它们是使用关键字public,protected或private之一定义的,后跟普通变量声明。 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();
  }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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