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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:50:06+00:00 2026-05-15T08:50:06+00:00

I’ve been having a hard time coming up a solution with this one. I’m

  • 0

I’ve been having a hard time coming up a solution with this one. I’m hoping you all can help me out.

Best described with an example:

class Parent {
    public $nationality;

    function __construct($nationality)
    {
        $this->nationality = $nationality
    }
}

class Child extends Parent {
    function __construct() {
        echo $this->nationality; // hispanic
    }
}

// Usage:
$parent = new Parent('hispanic');
$child = new Child();

I want the child to inherit properties and methods from a parent that is already initialized.


EDIT: Thanks all for the responses – let me give you some background. I’m trying to make a templating system. I have two classes – say Tag.php, and Form.php.

I’d like it to look like this:

class Tag {
    public $class_location;
    public $other_tag_specific_info;
    public $args;

    function __construct($args)
    {
        $this->args = $args;
    }

    public function wrap($wrapper) {
        ...
    }

    // More public methods Form can use.
}

class Form extends Tag {
    function __construct() {
        print_r($this->args()) // 0 => 'wahoo', 1 => 'ok'
        echo $this->class_location; // "/library/form/form.php"
        $this->wrap('form');
    }

    function __tostring() {
        return '<input type = "text" />';
    }
}

// Usage:
$tag = new Tag(array('wahoo', 'ok'));
$tag->class_location = "/library/form/form.php";
$tag->other_tag_specific_info = "...";
$form = new Form();

The reason I don’t like the composite pattern is that it doesn’t make
sense to me why I would be passing in an instance of Tag to the constructor
of one of its subclass, afterall – Form is a type of Tag right?

Thanks!
Matt Mueller

  • 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-15T08:50:06+00:00Added an answer on May 15, 2026 at 8:50 am

    We’ve edited quite a bit here already so I’ll just post a new answer, hope that’s ok. Will leave my old answer up for the sake of it.

    Anyway, we’re starting off from your latest example (Form extends Tag).

    Seems like you’re trying to mimic some kind of Builder pattern, so bear with me and let’s go with a short example:

    class TagBuilder
    {
        protected $args;
    
        protected $className;
    
        protected $classLocation;
    
        function __construct()
        {
    
        }
    
        public function setClass($file, $class)
        {
            /* @todo Use reflection maybe to determine if $class is a Tag subclass */
    
            if (file_exists($file))
            {
                require_once $file; // Or use the autoloader
            }
    
            $this->classLocation = $file;
            $this->className = $class;
        }
    
        /**
         * @return Tag the built tag (or tag subclass) instance
         */
        public function getTag()
        {
            $fullyQualifiedClassName = $this->getFullyQualifiedClassName();
    
            $newTag = new $fullyQualifiedClassName($this->args);
            $newTag->class_location = $this->classLocation;
    
            return $newTag;
        }
    
        protected function getFullyQualifiedClassName()
        {
            return $this->className;
        }
    
        public function setArgs($args)
        {
            $this->args = $args;
        }
    }
    

    Additionally, you will have to modify the Form constructor to call it’s parent::__construct($args) method in order to pass the $args dependency.

    And then run it with

    $builder = new TagBuilder();
    $builder->setClass('/library/form/form.php', 'Form');
    $builder->setArgs(array('wahoo', 'ok'));
    
    $form = $builder->getTag();
    

    This is a bit of a long example and it implies changing some of the OP’s API, but if I understood the question correctly, then this is the desired behaviour (or part of it) that the OP wants to mimic in his application.


    Note: I am aware that this isn’t a classical example of the Builder pattern and that the getTag method might seem a bit ambiguous to some purists.

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

Sidebar

Ask A Question

Stats

  • Questions 491k
  • Answers 491k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I need to write a standalone (not under XULRunner) c/c++… May 16, 2026 at 10:05 am
  • Editorial Team
    Editorial Team added an answer I figured it out. The object is returned afterall. It… May 16, 2026 at 10:05 am
  • Editorial Team
    Editorial Team added an answer Not sure I totally understand the problem, but this should… May 16, 2026 at 10:05 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm looking for suggestions for debugging... If you view this site in Firefox or
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.