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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:57:48+00:00 2026-06-03T13:57:48+00:00

I am working on a very simple template engine that would allow others to

  • 0

I am working on a very simple template engine that would allow others to extend the template parsing functionality via subclassing the TemplateParser class. The skeleton of my TemplateParser class looks like this:

abstract class TemplateParser {

    public static function parse_template($template_file) {
        //Do stuff with $template_file

        $specials = array();
        foreach (get_class_methods(__CLASS__) as $method) {
            if(strpos($method, "replace_") !== false) {
                $specials[] = $method;
            }
        }
    }

}

What I would like to do is be able to take a child class and add any number of replace_XXXXX methods in the child class that the parent “automatically” knows about. My problem is that the __CLASS__ constant is always equal to ‘TemplateParser’, even when called on a child class. Is there any way that I can get the methods of the child class from within TemplateParser?

  • 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-03T13:57:49+00:00Added an answer on June 3, 2026 at 1:57 pm

    If you’re going to use a static method why even bother asking users to extend the parent class?

    OOP vs. COP

    First, what you’re suggesting is not OOP — it’s COP (Class Oriented Programming). I’d advise you to consider exactly why you’ve made TemplateParser::parse_template static in the first place. Is there a really, really good reason (hint: not likely)? Just because PHP 5.3 introduced late-static binding doesn’t mean you should use it willy-nilly all over the place. In fact, static is rarely the best option.

    Composition Over Inheritance

    Second, your stated use-case doesn’t provide any compelling reason for using inheritance. You should almost always favor composition over inheritance. Consider:

    interface ParserInterface
    {
        public function parse_template($template_file);
    }
    
    interface ReplacerInterface
    {
        // fill in your own interface requirements here
    }
    
    class Parser implements ParserInterface
    {
        private $replacer;
    
        public function __construct(ReplacerInterface $replacer)
        {
            $this->replacer = $replacer;
        }
    
        public function parse_template($template_file)
        {
            $specials = array_filter(function($method) {
                return strpos($method, "replace_") === 0;
            }, get_class_methods($this->replacer));
    
            foreach ($specials as $method) {
                $this->replacer->$method($template_file);
            }
        }
    }
    

    In the above code we’re able to reap all the advantages of Dependency Injectionwiki and our code is imminently more testable, maintainable and less prone to breakage than had we used a convoluted class-oriented implementation with static.

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

Sidebar

Related Questions

I'm working on a very simple iPhone app, that in the end will have
I'm doing something very simple that's just not working. I have an html page
I have a very simple case that I think would benefit from using templates
I am working on a very simple app that needs to listen on a
I'm working with a very simple web service that uses a base class to
I have a listbox that uses a data template. The template is very simple
I'm working on a very simple first step of a project I'm doing: a
I am working on a very simple stopwatch using WPF but using the System.Diagnostics
I'm working on a very simple game (essentially an ice sliding puzzle), for now
I've been working on a very simple crud generator for pylons. I came up

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.