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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:26:31+00:00 2026-05-13T15:26:31+00:00

The idea is to create a DOM-like tree. But there are some restrictions, that

  • 0

The idea is to create a DOM-like tree. But there are some restrictions, that only certain types can actually contain the other.

I want to use an interface|abstract class|superclass to implement some well known js-functions as appendChild, replaceChild etc.

I’m using the classes page, block and item, where pages can contain blocks and blocks can contain either blocks or items.

Example: Page is a web page, block could be an list element and item could be an list item element.

But these objects contain more than just html-data and the concepts goes beyond just plain HTML representation. It’s an overall idea of managing items, wether they have an actual representation or are just abstract objects. The concept itself works for many different hierarchies.

What I want to achieve is to reuse as much code of the parent class as possible (adding a child is basically the same for all classes) but to differ the type hints to match the allowed types to add as a child.

There are basically four ways I found out myself:

  1. I use an interface, which allows me to type hint to the superclass but not to change these.
  2. I use a superclass with public methods, so i can redefine the type hints (which is totally against usual practices when heriting preconditions).
  3. I use a superclass with protected methods, which seems still being quite quirky.
  4. I get rid of any superclass and just define almost the same class several times.
  5. I use a method to check for the type, despite the feature of type hints.

So, if anyone is still willing to answer I’m happy for any proposition, idea or hint, which option to choose. I hope I could describe the issue well enough.

And if there is something i missed I’m thankful to hear it 😉

Code

Superclass way (works, but breaks precondition inheriting practice)

class Base {

    public|protected function appendChild(Base $child) {
        // do stuff
    }
}

class Block extends Base {

   public function appendChild(Block $child) {
       parent::appendChild($child);
   }

}

Interface way (Does not work. It must not)

interface Interface1 {

    public function appendChild(Base $child);

}

class Base implements Interface1 {

    public|protected function appendChild(Base $child) {
        // do stuff
    }
}

class Block extends Base{

   public function appendChild(Block $child) {
       parent::appendChild($child);
   }

}

Edited parts are bold

  • 1 1 Answer
  • 3 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-13T15:26:31+00:00Added an answer on May 13, 2026 at 3:26 pm

    Interface makes most sense to me. You can have one class that plays multiple roles, as it can implement multiple interfaces.

    // you don't need any methods in the interfaces
    class Foo implements Inline, Block {} 
    

    will work with both:

    appendChild(Inline $foo); and appendChild(Block $foo);
    

    and interfaces can extend each other, so there can be common interface for all your objects.

    You can still use inheritance to reuse implementation, and you’ll have flexibility to use inhertiance tree strictly for reuse of implementation, not limited by your page logic (you’ll never be forced to make StaticSimpleton extend HeavyDatabaseyWidget).

    If not interfaces, I’d go for option 5: just make appendChild call $child->canBeChildOf($this) and/or $this->accepts($child). Again, logic and implementation will be independent, and you’ll have a lot of freedom with your logic.

    PHP does type checks at run tmie, so use of type system doesn’t buy you much anyway.

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

Sidebar

Related Questions

I'm not sure if its the best idea to create a background job that
Working with Unity, I thought it would be a good idea to create some
My idea is to create a .jar file that contains Services managed by Spring,
idea I would like to create a little app for myself to store ideas
I am trying to create an OSGI project on IntelliJ Idea but so far
Does anyone have any idea on how can you create a product filtering query
I've spent whole days with PHP's DOM functions but i can't understand how it
I have some external Javascript files in my GWT app that I only want
I am trying to create a webpage where users can insert a pin (like
I noticed that when I do something like this (with jQuery, but I don't

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.