how to force implementation of an abstract class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    how to force implementation of an abstract class?

    I've got 2 DB classes (see below) where one class extends my base DB class. I want the child classes (the User class is just an example) to have the static method getInstance() but I can't declare this function abstract (E_STRICT) and I don't see, how I can implement an interface.

    does anyone have an idea how to solve that?

    Code:
    abstract class Base
    {
    	private static $PDO = NULL;
    	private static $PS  = array();
    	
    	function __destruct()
    	{
    		self::$PDO = NULL;
    	}
    	
    	protected static function load()
    	{
    		if (self::$PDO === NULL)
    		{
    			try {
    				$dsn = 'mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME_2;
    				self::$PDO = new PDO($dsn, DB_USER, DB_PASS);
    				self::$DBstatus = 'connected';
    			}
    			catch (PDOException $pdo)
    			{
    				ErrorLog::logException($pdo, __CLASS__, __METHOD__);
    			}
    		}
    	}
    
    	// some more general and useful methods
    
    }
    Code:
    class User extends Base
    {
    	private static $instance = NULL;
    	
    	function __construct() {}
    	
    	function __clone() {}
    
    # I want this function to be implemented
    # by every child class of Base
    # though not necessarily with the self::init() call
    	
    	public static function getInstance()
    	{
    		if (self::$instance === NULL)
    		{
    			self::$instance = new self;
    			// this is where class specific code begins
    			self::init();
    		}
    		return self::$instance;
    	}
    	
    	private static function init()
    	{
    		parent::load();
    		# prepare several statements
    	}
    
    }
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    I'm kind of lost in what you're trying to accomplish. May I ask how you came to the conclusion you need this?

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      sure, I need to make sure the DB class I use is a Singleton. and I want the connector class as (kind of) parent.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        You could simply have all child classes implement an interface, as well as expand the base class.

        Then they would both inherit the requirements from the base class, and be required to implement everything from the interface.

        For example:
        [code=php]
        <?php
        // Interfaces for the base and the child classes.
        interface iBase {
        public function connect();
        }
        interface iBaseChild {
        public static function getInstance();
        }

        // Base class
        abstract class Base implements iBase {
        public function connect() {
        echo "Base connect" . PHP_EOL;
        }
        }

        // First Child class
        class FirstChild extends Base implements iBaseChild {
        public static function getInstance() {
        return new FirstChild();
        }
        }

        // Second Child class
        class SecondChild extends Base implements iBaseChild {
        // Will simply inherit the connect function from the parent
        // but fail because no getInstance function was defined.
        }
        ?>[/code]
        The second child class will fail, because it does not define your static getInstance method.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Thanks, that's clearing the matter (I didn't know that you can extend and implement at the same time).

          do you know if you can force a private/protected static method to be implemented?

          Comment

          • bilibytes
            New Member
            • Jun 2008
            • 128

            #6
            Originally posted by Dormilich
            do you know if you can force a private/protected static method to be implemented?
            Did you find out ?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              kind of. you can't. simple reason is because they’re not public (interfaces only deal with public methods) and you can’t make them abstract because they’re static.

              Comment

              Working...