Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:30:43+00:00 2026-05-31T00:30:43+00:00

I am trying to create an class object to a class but I keep

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:30:45+00:00Added an answer on May 31, 2026 at 12:30 am

    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

    • http://martinfowler.com/articles/injection.html
    • http://en.wikipedia.org/wiki/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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an instance of a generic class using a Type object.
I am trying to create a class in VB.NET which inherits a base abstract
I'm trying to create a point class which defines a property called coordinate. However,
I am trying to create a generic class which new's up an instance of
I'm trying to make creating new instances with Tastypie work, but I keep getting
I'm trying to create a menu in Qt following this example http://doc.qt.nokia.com/latest/mainwindows-menus.html but I
I am trying to create an array of class objects taking an integer argument.
I am tryıng to create new object of other class ın a for loop.
I am trying to create a class that implements the IUnknown interface. I have
I'm trying to create a class that can instantiate arrays at runtime by giving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.