473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to force implementation of an abstract class?

Dormilich
8,658 Expert Mod 8TB
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?

Expand|Select|Wrap|Line Numbers
  1. abstract class Base
  2. {
  3.     private static $PDO = NULL;
  4.     private static $PS  = array();
  5.  
  6.     function __destruct()
  7.     {
  8.         self::$PDO = NULL;
  9.     }
  10.  
  11.     protected static function load()
  12.     {
  13.         if (self::$PDO === NULL)
  14.         {
  15.             try {
  16.                 $dsn = 'mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME_2;
  17.                 self::$PDO = new PDO($dsn, DB_USER, DB_PASS);
  18.                 self::$DBstatus = 'connected';
  19.             }
  20.             catch (PDOException $pdo)
  21.             {
  22.                 ErrorLog::logException($pdo, __CLASS__, __METHOD__);
  23.             }
  24.         }
  25.     }
  26.  
  27.     // some more general and useful methods
  28.  
  29. }
Expand|Select|Wrap|Line Numbers
  1. class User extends Base
  2. {
  3.     private static $instance = NULL;
  4.  
  5.     function __construct() {}
  6.  
  7.     function __clone() {}
  8.  
  9. # I want this function to be implemented
  10. # by every child class of Base
  11. # though not necessarily with the self::init() call
  12.  
  13.     public static function getInstance()
  14.     {
  15.         if (self::$instance === NULL)
  16.         {
  17.             self::$instance = new self;
  18.             // this is where class specific code begins
  19.             self::init();
  20.         }
  21.         return self::$instance;
  22.     }
  23.  
  24.     private static function init()
  25.     {
  26.         parent::load();
  27.         # prepare several statements
  28.     }
  29.  
  30. }
Apr 6 '09 #1

✓ answered by Atli

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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Interfaces for the base and the child classes.
  3. interface iBase {
  4.     public function connect();
  5. }
  6. interface iBaseChild {
  7.     public static function getInstance();
  8. }
  9.  
  10. // Base class
  11. abstract class Base implements iBase {
  12.     public function connect() {
  13.         echo "Base connect" . PHP_EOL;
  14.     }
  15. }
  16.  
  17. // First Child class
  18. class FirstChild extends Base implements iBaseChild {
  19.     public static function getInstance() {
  20.         return new FirstChild();
  21.     }
  22. }
  23.  
  24. // Second Child class
  25. class SecondChild extends Base implements iBaseChild {
  26.     // Will simply inherit the connect function from the parent
  27.     // but fail because no getInstance function was defined.
  28. }
  29. ?>
The second child class will fail, because it does not define your static getInstance method.

6 4038
dlite922
1,584 Expert 1GB
I'm kind of lost in what you're trying to accomplish. May I ask how you came to the conclusion you need this?
Apr 7 '09 #2
Dormilich
8,658 Expert Mod 8TB
sure, I need to make sure the DB class I use is a Singleton. and I want the connector class as (kind of) parent.
Apr 7 '09 #3
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Interfaces for the base and the child classes.
  3. interface iBase {
  4.     public function connect();
  5. }
  6. interface iBaseChild {
  7.     public static function getInstance();
  8. }
  9.  
  10. // Base class
  11. abstract class Base implements iBase {
  12.     public function connect() {
  13.         echo "Base connect" . PHP_EOL;
  14.     }
  15. }
  16.  
  17. // First Child class
  18. class FirstChild extends Base implements iBaseChild {
  19.     public static function getInstance() {
  20.         return new FirstChild();
  21.     }
  22. }
  23.  
  24. // Second Child class
  25. class SecondChild extends Base implements iBaseChild {
  26.     // Will simply inherit the connect function from the parent
  27.     // but fail because no getInstance function was defined.
  28. }
  29. ?>
The second child class will fail, because it does not define your static getInstance method.
Apr 8 '09 #4
Dormilich
8,658 Expert Mod 8TB
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?
Apr 8 '09 #5
bilibytes
128 100+
@Dormilich
Did you find out ?
Jul 26 '09 #6
Dormilich
8,658 Expert Mod 8TB
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.
Jul 26 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
4
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real...
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
21
by: Steve - DND | last post by:
I know it's possible to require inheriting classes to implement a particular function or property, but is it possible to require a inheriting class to implement a constructor of it's own? ...
5
by: Keith Patrick | last post by:
Could someone tell me if it's possible (and if so, how) to call an explicitly-implemented interface method from a subclass? I have a class in which I have to explicity implement some methods, but...
6
by: Mark Broadbent | last post by:
this might sound like an obvious question but I have found that usually these two evolve at the same time. One of the biggest reasons for creating the abstraction in the first place (in my...
7
by: AWHK | last post by:
How can I force anyone who subclasses my class to override the ToString() method? Andreas :-)
7
by: tron.thomas | last post by:
Please consider the following code: class Abstract { public: virtual ~Abstract() {} virtual void Method() = 0; }; class Concrete : public virtual Abstract
2
by: Ben Voigt | last post by:
The C# Language Specification says: A virtual property declaration specifies that the accessors of the property are virtual. The virtual modifier applies to both accessors of a read-write...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.