Limit which classes can distribute another class

I am currently writing a solution containing some abstract classes. One of them should only be expanded from a few other classes, as using it from other places than intended could cause a bit of confusion. Therefore, I want to limit which classes can extend this class.

So, if I have a class called ImportantStuff, which is abstract, I want to say that only class A and class B can extend it, and any other class cannot.

You may wonder why I would like to do something like this. Well, the code is written to work, so later there will be many other programmers, so I want to make it clear to those who this class is not intended to be used from any other places than it is. Yes, I know that I can write this in the class description, and I have it, but I want to make it 100% understandable by blocking it, because not all programmers can read comments :) I’m also curious if this can be done in good fashion, because I could not find a good answer on the Internet.

So my question is: what is the best way to do this? I think I can send a key or something that is being processed in the constructor, but is there a cleaner way to do this?

+3
source share
4 answers

hmmm, ( init) , , thats possbilie, 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();
+4

PHP 5.3+:

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;

:



Toto

NB: , .

+3

, . .

, , . , , .

, , . , . . , . : , , .

+1

, .

enter a class namelist of it extendable classin a line like this

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

Then use this data in your code to find out if any class can expand

Maybe the sample will be

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

you can also use a simple file or xml to store the class name instead of the database or to store them in multidimensional-arraycan also be done ...

0
source

Source: https://habr.com/ru/post/1783579/


All Articles