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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T12:59:05+00:00 2026-05-28T12:59:05+00:00

In the development of a website, I encountered this problem. I want to pass

  • 0

In the development of a website, I encountered this problem. I want to pass an object into a class, and afterwards I’d like to use it’s methods. Here’s what I’m trying to do:

[updated code below]

First I create a new theme and header. Then I’d like to use the newly created $theme in my Header class. So I need to pass it. I want to use the same instance of $theme in multiple classes, so I can’t create a new one. Also I’d like to avoid using a Singleton.

With my current code I’m getting this error:

Fatal error: Call to a member function getHeader() on a non-object in...

My questions:

  • Is this approach going to work or is it completely wrong?
  • How can I pass an object to another object and then still be able to use it’s methods?
  • Might it be better to use a singleton instead and use Theme::getInstance(); to use it in the other class?

[edit]
More detailled code:

$theme = new Theme($db);
$builder = new Builder($login, $db, $theme);
$builder->build();

Builder.php:

class Builder {
    private $login;
    private $db;
    private $theme;

    public function __construct($login, $db, $theme){
        $this->login = $login;
        $this->db = $db;
        $this->theme = $theme;
    }

    public function build(){
        $this->buildHeader();
        $this->buildContent();
        $this->buildFooter();
    }

    public function buildHeader(){
        $header = new HeaderBuilder($this->login, $this->db);
        $header->setTheme($this->theme);
        $header->render();
    }

    public function buildContent(){}
    public function buildFooter(){}
}

Abstract builder class:

abstract class AbstractBuilder {
    private $variable = array();
    private $login;
    private $db;
    private $view;

    abstract function build();

    public function __construct($login, $db){
        $this->login = $login;
        $this->db = $db;
        $this->build();
    }

    public function render(){
        extract($this->variable);
        include($this->view);
    }
}

HeaderBuilder:

class HeaderBuilder extends AbstractBuilder {
    private $theme;

    public function build(){
        $this->view = $this->theme->getHeader();
    }

    public function setTheme($theme){
        $this->theme = $theme;
    }       
}
  • 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-28T12:59:06+00:00Added an answer on May 28, 2026 at 12:59 pm

    Your approach works fine in the following code:

    $theme = new Theme();
    $header = new Header();
    $header->setTheme($theme);
    $header->build();
    
    class Header {
        private $theme;
    
        public function setTheme($theme){
            $this->theme = $theme;
        }
    
        public function build(){
            $this->view = $this->theme->getHeader();
        }
    }
    
    class Theme {
        public function getHeader() {
            echo 'yes';
        }
    }
    

    Perhaps there’s something else that’s stopping it from working?

    EDIT

    A quick spot, you mistyped construct:

    public function __constuct($login, $db, $theme){
        $this->login = $login;
        $this->db = $db;
        $this->theme = $theme;
    }
    

    EDIT2 I found your error:

    When new HeaderBuilder() gets called:

        $header = new HeaderBuilder($this->login, $this->db);
    

    $this->build(); gets executed in your AbstractBuilder:

    public function __construct($login, $db){
        $this->login = $login;
        $this->db = $db;
        $this->build();
    }
    

    Which points to

    public function build(){
        $this->view = $this->theme->getHeader();
    }
    

    In your HeaderBuilder.. BUT! That $this->build(); is being called before the $header->setTheme() is being called, so the $theme variable in HeaderBuilder is empty.
    Commenting out that $this->view = … line makes the code work again.

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

Sidebar

Related Questions

I am trying to use the FitBit API at website development, I want to
Most designers use 1024x768 as a baseline for website development. That allows them to
Should we only use Web-Safe Colors while in design/coding/development for website/apps?
So for development purposes, we had our folder structure for our website something like
This morning I wanted to move my development website online (in a protected folder),
I'm kind of a website development nub so bear with me. My problem is
I use textmate for website development and compass to compile css stylesheets. Right now
I use Eclipse for PHP / MySQL / JS website development. From Eclipse 4.2.1
I'm hoping someone have encountered this problem before and will be able to help
I've recently taken over development of a website written in asp.net. Before taking this

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.