0

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

3 Answers 3

4

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. e.g. call methods (which, of course, have to be defined) on it or pass it to other objects.

Sign up to request clarification or add additional context in comments.

5 Comments

hey, I did not know that. But is strange I had seen this kind of example in a book, and they did it outside of the constructor or not even inside a method?
@Eircman Maybe they assigned literal values to class properties. I'll update the example.
thank you so much. Do I have to do that only in a constructor or any method? For example if I just want to print now how would I call that method?
@Eircman I updated the answer. Though, honestly, I don't quite get what you're asking. ;)
Hey thank you so much, don't worry I figured out what I needed to do. I was wondering how you actually call a method to do something, since you can't just call it if it is in another method. But I understood that I don't need to use class to print this, in my index.php for example.
1

Your businessLogic class is not defined correctly.

<?php

include 'helperClass.php';

class busniessLogic {

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

$bLogic = new businessLogic();

?>

Comments

1

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();
  }

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.