January 5, 2011

Limit what classes can extends another class

Question by Sondre

I’m currently writing a solution that contains some abstract classes. One of these should only be extended from a few other classes as using it from other places than intended could cause a little bit of a mess. So what I want to do is limit what classes can extend this class.

So if I have a class named ImportantStuff which is abstract I want to say that only Class A and Class B are able to extend it while any other class can’t.

You might wonder why I would want to do something like this. Well the code is written for work so there will be lots of other programmers working with it later, so I wish to make it clear for those that this class is not intended to be used from any other places than it already is. Yes I know I can write it in a class description and I have, but I want to make it 100% clear by blocking it as not all programmers are good at reading comments:) Also I’m kinda curious if it can be done in a good fashion as I couldn’t find a good answer on the web.

So my question is: what is the best way to make this happen? I guess I can send a key or something that is handled in the constructor, but is there a cleaner way to do this?

Answer by Hannes

hmmm, make the constructor final (but add an init function) and check there which class is currently constrcuted, thats possbilie, but requires at least php 5.3

    abstract class bigOne{
        private $_allowedClasses = array('smallOne');

        final public function __construct() {
            if(!in_array(get_called_class(), $this->_allowedClasses)){
                throw new Exception('can not extend to Class: ' . get_called_class());
            }
            $this->init();
        }

        abstract public function init();
    }

    class smallOne extends bigOne{
        public function init(){

        }
    }

    class badOne extends bigOne{
        public function init(){

        }
    }

    $oSmallOne = new smallOne();
    $obadOne = new badOne();

Answer by Starx

I suppose you can use database to accomplish this.

put class name the list of its extendable class in a row like this

-------------------------------------------------------------------
Class       |       Extendable class
-------------------------------------------------------------------
child 1     |       parent1,parent2,parent3
---------------------------------------------------------------

Then use this data in your code to see if a certain class is extendable

May be a sample will be this

$extendableClass = explode(',',$datarow['extendable class']);
$toextendClass = "parent4";
if(in_array($toextendClass,$extendableClass)) {
    //extend
}
else { 
    //dont extend
}

you can also use plain file or xml to store the class name instead of database or storing them in multidimensional-array might also get this done….

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!