Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit what classes can extends another class

Tags:

php

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?

like image 763
Sondre Avatar asked Dec 15 '25 10:12

Sondre


2 Answers

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();
like image 54
Hannes Avatar answered Dec 17 '25 00:12

Hannes


Possible solution for PHP 5.3+ only:

class Toto {
    function __construct() {
        // list of classes that can extend Toto, including itself obviously:
        if(!in_array(get_called_class(), array(__CLASS__, 'Foo')))
            exit(get_called_class() . " cannot extend " . __CLASS__ . "\n");

        echo "Success\n";
    }
}

class Foo extends Toto {}
class Bar extends Toto {}

new Toto;
new Foo;
new Bar;

Which returns:

Success
Success
Bar cannot extend Toto

NB: this does not work if the constructor is overwritten.

like image 26
seriousdev Avatar answered Dec 16 '25 22:12

seriousdev