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

PHP从另一个类中的类创建对象,php,class,object,Php,Class,Object,我正试图为一个类创建一个类对象,但我一直收到一些未知错误 这是“helper类”,它从XML文件中获取内容 <?php class helperClass { private $name; private $weight; private $category; private $location; //Creates a new product object with all its attributes function __cons

我正试图为一个类创建一个类对象,但我一直收到一些未知错误

这是“
helper类”
,它从
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();
    }
  ?>
}
在你们的帮助下,我明白了,这就是我想要做的

<!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>

您的第二个代码段错误。不能在类def中计算代码。仅在类的方法内部。 尝试将代码放入构造函数中:

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;
  }
}
这只是一个如何在对象创建时执行代码的示例

虽然我不会这么用。我宁愿将对象传入或稍后设置

参考:依赖项注入


一旦对象被创建,你可以用它做任何你想做的事情。e、 g.对其调用方法(当然,必须定义这些方法)或将其传递给其他对象。

您的第二个代码段是错误的。不能在类def中计算代码。仅在类的方法内部。 尝试将代码放入构造函数中:

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;
  }
}
这只是一个如何在对象创建时执行代码的示例

虽然我不会这么用。我宁愿将对象传入或稍后设置

参考:依赖项注入


一旦对象被创建,你可以用它做任何你想做的事情。e、 g.对其调用方法(当然,必须定义这些方法)或将其传递给其他对象。

您的businessLogic类定义不正确

<?php

include 'helperClass.php';

class busniessLogic {

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

$bLogic = new businessLogic();

?>

您的businessLogic类定义不正确

<?php

include 'helperClass.php';

class busniessLogic {

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

$bLogic = new businessLogic();

?>

您试图做的是错误的,因为(取自文档)

类成员变量称为“属性”。你也可以看到它们 指使用其他术语,如“属性”或“字段”,但 在本参考中,我们将使用“属性”。他们是 通过使用关键字public、protected或private之一定义, 后跟一个普通变量声明。本声明可以 包括初始化,但此初始化必须是常量 值——也就是说,它必须能够在编译时进行计算,并且 必须不依赖运行时信息才能进行评估

所以你应该做一些类似的事情

class busniessLogic {
   private $obj;

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

 }

你试图做的是错误的,因为(摘自文件)

类成员变量称为“属性”。你也可以看到它们 指使用其他术语,如“属性”或“字段”,但 在本参考中,我们将使用“属性”。他们是 通过使用关键字public、protected或private之一定义, 后跟一个普通变量声明。本声明可以 包括初始化,但此初始化必须是常量 值——也就是说,它必须能够在编译时进行计算,并且 必须不依赖运行时信息才能进行评估

所以你应该做一些类似的事情

class busniessLogic {
   private $obj;

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

 }

嘿,我不知道。但奇怪的是,我在一本书中看到过这样的例子,他们在构造函数之外,甚至在方法内部都没有这样做?@Eircman也许他们给类属性分配了文本值。我将更新示例。非常感谢。我必须只在构造函数或任何方法中这样做吗?例如,如果我现在只想打印,我将如何调用该方法?@Eircman我更新了答案。不过,老实说,我不太明白你的要求嘿,非常感谢你,别担心,我知道我需要做什么了。我想知道你实际上是如何调用一个方法来做一些事情的,因为如果它在另一个方法中,你不能仅仅调用它。但我知道我不需要使用类来打印它,例如在我的index.php中。嘿,我不知道这一点。但奇怪的是,我在一本书中看到过这样的例子,他们在构造函数之外,甚至在方法内部都没有这样做?@Eircman也许他们给类属性分配了文本值。我将更新示例。非常感谢。我必须只在构造函数或任何方法中这样做吗?例如,如果我现在只想打印,我将如何调用该方法?@Eircman我更新了答案。不过,老实说,我不太明白你的要求嘿,非常感谢你,别担心,我知道我需要做什么了。我想知道你实际上是如何调用一个方法来做一些事情的,因为如果它在另一个方法中,你不能仅仅调用它。但我知道我不需要使用类来打印它,例如在我的index.php中。