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 8985095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:15:31+00:00 2026-06-15T21:15:31+00:00

After hours looking for solutions and patterns, it’s time for me to ask the

  • 0

After hours looking for solutions and patterns, it’s time for me to ask the pros.

I’d love to order my object in a logical hierarchy, but still want to be able to access properties of the parent object. A simple example how I’d love to have it work…

    class car {

       public $strType;  // holds a string
       public $engine;   // holds the instance of another class

       public function __construct(){
           $this->type = "Saab";
           // Trying to pass on $this to make it accessible in $this->engine
           $this->engine = new engine($this);
       }

    }

    class engine {

        public $car;

        public function __construct($parent){
            $this->car = $parent;
        } 

        public function start(){
            // Here is where I'd love to have access to car properties and methods...
            echo $this->car->$strType;
        }
    }

    $myCar = new car();
    $myCar->engine->start();

What I won’t achieve is that a method in engine can access the ‘parent’ car properties.
I managed to do so like this, but I believe that is very very ugly…

    $myCar = new car();
    $myCar->addParent($myCar);

From within the addParent MethodI would be able to pass the instance on to the engine object.
But that can’t be the clue, can it? Is my whole idea queer?

I don’t want engine to inherit from car because a car has lots of methods and properties and engine has not. Hope you get what I mean.

Hoping for hints,
Cheers Boris

  • 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-06-15T21:15:32+00:00Added an answer on June 15, 2026 at 9:15 pm

    As @Wrikken mentioned, the correct syntax would be echo $this->car->strType;

    type does not seem to be a member of car, but if you changed it the line to

    $this->strType = "Saab";
    

    Then the statement in question should now echo “Saab”

    Though I think good practice here would be to not have the engine class contain a car object, just the car class should contain an engine object. And the properties would be better off as private. So you could have a car method like

    public startEngine() {
        $success = $this->engine->start();
        if(success) {
            echo "Engine started successfully!";
        } else {
            echo "Engine is busted!";
        }
    }
    

    Where engine::start() returns a boolean.

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

Sidebar

Related Questions

This is either dead simple or can't be done, but after hours of looking
After some hours of trying, I want to ask how to loop a video
im trying to customize this page indicator with half succes, but after many hours
I'm embarrassed by the 'basicness' of this question, but after wasted hours, here goes.
I'm trying to use a simple UPDATE query with pdo but after 5 hours
I'm really new to python and after several hours of looking I haven't been
I want to store some information in the local browser cookie. After hours looking
after hours of googeling and searching within SO, I finaly come to the place
after hours of tracking mysterious one or two seconds long lasting freeze I finally
After hours I am giving up on debugging the following: This works: URL[] urls

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.