【问题标题】:PHP create an object from a class inside another classPHP从另一个类中的一个类创建一个对象
【发布时间】:2012-03-09 10:46:20
【问题描述】:

我正在尝试为一个类创建一个类对象,但我不断收到一些未知错误。

这是“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();
    }
  ?>
}

在这里,我试图从helperClassclass 创建一个对象,并从helperClass 调用方法listProducts,但是如果我尝试实例化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;
    }
}
}
?>

在你们的帮助下,我想通了,这就是我想做的事

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

【问题讨论】:

    标签: php class object


    【解决方案1】:

    您的第二个代码 sn-p 错误。您无法评估类 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;
      }
    }
    

    这只是一个如何在对象创建时执行代码的示例。

    虽然我实际上不会以这种方式使用它。我宁愿将对象传入或稍后设置。

    ref依赖注入

    创建对象后,您可以对它做任何您想做的事情。例如在其上调用方法(当然,必须定义)或将其传递给其他对象。

    【讨论】:

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

    您的 businessLogic 类定义不正确。

    <?php
    
    include 'helperClass.php';
    
    class busniessLogic {
    
      function __construct() {
        $obj = new helperClass();
        $obj->listPruduct();
      }
    }
    
    $bLogic = new businessLogic();
    
    ?>
    

    【讨论】:

      【解决方案3】:

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

      类成员变量称为“属性”。您可能还会看到它们 提及使用其他术语,例如“属性”或“字段”,但 出于本参考的目的,我们将使用“属性”。他们是 使用关键字 public、protected 或 private 之一定义, 然后是一个普通的变量声明。该声明可 包括一个初始化,但这个初始化必须是一个常量 价值——也就是说,它必须能够在编译时被评估,并且 必须不依赖运行时信息才能被评估

      所以你应该做类似的事情

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-23
        • 1970-01-01
        相关资源
        最近更新 更多