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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:06:52+00:00 2026-06-13T12:06:52+00:00

I recently began to develop in php5 in an object oriented way and I’m

  • 0

I recently began to develop in php5 in an object oriented way and I’m stuck at something. I would really appreciate your help/recommendations.

Bear with me in this since it ended up in a mess 🙁

This is my scenario (hope I can elaborate on this clearly): I have two dynamic classes, Client and Supplier which use methods of a static class called Vocabulary. Vocabulary is a class that pulls vocabulary terms from a source which can be: plain text file, mongodb database or mysql database. An entry in a configuration file determines which of the
aforementioned three types of sources the application will use.

class Client {
    public function foo() {
        ...
        Vocabulary::getTerm();
        ...
    }
    ...
}

class Supplier {
    public function bar() {
        ...
        Vocabulary::getTerm();
        ...
    }
    ...
}

class Vocabulary {
    public static function useVocab($vocab) {
        $_SESSION['vocab'] = $vocab;
    }
    public static function getTerm($termKey) {...}
    ...
}

I planned to create Vocabulary child classes for each of the types I want to support, for example: Vocabulary_file, Vocabulary_mongodb and Vocabulary_mysql.
Vocabulary_file will override its parent useVocab() because it needs to perform additional operations appart from setting the $_SESSION variable, but
Vocabulary_mongodb and Vocabulary_mysql don’t need to override their useVocab() parent method (they just need the $_SESSION variable set).
All three Vocabulary “child” classes will override getTerm() method.

The following is what I tried and this is the mess I ended up with 🙁

  • For Vocabulary_mongodb and Vocabulary_mysql, since useVocab() doesn’t exist but is inherited from Vocabulary, “method_exists()” returns true and that call
    causes an infinite loop.
  • I looks weird both calling the child explicitly in Vocabulary and calling the parent:: in the child class.

After lots of cups of coffee I have exhausted all my wits and my brain is damaged.

// Class Vocabulary modified to make it call the desired "child" class too
class Vocabulary {
    // This would execute "child" class method
    private static function _callChild($method, $args) {
        $child_class = 'Vocabulary_' . Config::$vocab['type']; // Config::$vocab['type']     can be: file, mongodb or mysql
        if (method_exists($child_class, $method)) {
            return call_user_func_array($child_class . '::' . $method, $args);
        } else {
            return false;
        }
    }
    public static function useVocab($vocab) {
        $_SESSION['vocab'] = $vocab;
        self::_callChild(__FUNCTION__, compact('vocab'));
    }
    public static function getTerm($termKey) {
        $termKey = strtolower($termKey);
        self::_callChild(__FUNCTION__, compact('termKey'));
    }
    ...
}

class Vocabulary_file extends Vocabulary {
    public static function useVocab($vocab) {
        parent::useVocab($vocab);
        // some specific stuff here
    }
    public static function getTerm($termKey) {
        parent::getTerm($termKey);
        // some specific stuff here
    }
}

class Vocabulary_mongodb extends Vocabulary {
    public static function getTerm($termKey) {
        parent::getTerm($termKey);
        // some specific stuff here
    }
}

class Vocabulary_mysql extends Vocabulary {
    public static function getTerm($termKey) {
            parent::getTerm($termKey);
        // some specific stuff here
    }
}

I would like to know how can I design the Vocabulary classes in order to keep the Vocabulary::… like calls in Client and Supplier and let Vocabulary know which child class use for the type configured in “Config” class.

Any advice will be greatly appreciated.
Cheers

  • 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-13T12:06:53+00:00Added an answer on June 13, 2026 at 12:06 pm

    If you’re using all static methods, you may as well not use OOP at all, it’s basically all just global function calls. If you want inheritance with polymorphism to work, you pretty much need to instantiate your classes. The polymorphism then comes from the fact that the instantiated objects can be anything, but you’re calling the same methods on them. E.g.:

    abstract class Vocabulary {
    
        abstract public function getTerm($termKey);
    
    }
    
    class Vocabulary_File extends Vocabulary {
    
        public function getTerm($termKey) {
            // get from file
        }
    
    }
    
    class Vocabulary_MySQL extends Vocabulary {
    
        public function getTerm($termKey) {
            // get from database
        }
    
    }
    

    You can use this polymorphic like this:

    if (mt_rand(0, 1)) {
        $vocab = new Vocabulary_File;
    } else {
        $vocab = new Vocabulary_MySQL;
    }
    
    // This call is polymorphic.
    //  What exactly it does depends on which class was instantiated.
    $vocab->getTerm('foo');
    

    This is how polymorphism is really useful. The interface (getTerm($termKey)) is defined and unchanging between classes, but the specific implementation changes. If your code is hardcoding calls to Vocabulary::getTerm(), that’s not polymorphism. With your structure you’re also violating an important OO design rule: The parent does not know about its children, and it does not interact with its children. The children override functionality of the parent, not the other way around.

    You also shouldn’t use the $_SESSION as a form of global storage. Keep objects self contained.

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

Sidebar

Related Questions

I would appreciate your help with a hopefully trivial issue. Recently I've started learning
I began studying Java Regular Expression recently and I found some really intersting task.For
I recently began attempting to develop a simple Wordpress theme for myself, and have
i've just recently began to develop using kivy for testing purposes and build the
I recently began working on a large ASP.NET WebForms project. The project was using
I recently began working on a large project that contains a huge number of
I recently began to read some F# related literature, speaking of Real World Functional
I recently began using github and started playing with eclipse's git abilities, using some
I have recently began using C# to invoke powershell scripts, with some success :)
I've recently began using C++ with XCode and I'm starting to miss the integrated

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.