Re: interfaces - your opinion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hi Marco,

I want to apply the idea to interface implementation not
class extension. that is to say I want to be able to make an
interface only implementable by certain (defined) classes (and their
subclasses) with out imposing any restriction on the classes
themselves (like that they must extend a certain base class in order
to have the 'implementable by' check performed.

IFAICT this is not really possible - but maybe somebody sees a way,
then again maybe it's too silly an idea to even consider it.
regardless thanks for your feedback.

rgds,
jochem

Marco Kaiser wrote:
Hi Jochem,

try this.

<?php

abstract class AClass
{
    private $allowedClasses = array(
                                'MyClass',
                                'MyOtherClass'
                                );
    final public function __construct()
    {
        if (!in_array(get_class($this), $this->allowedClasses)) {
            throw new Exception('not allowed classes: ' . __CLASS__);
        }
// if (($this instanceof MyClass) ||
//            ($this instanceof MyOtherClass)) {
//            throw new Exception('not allowed class: ' . __CLASS__);
//        }
    }
}


class MyClass extends AClass {}

class MyOtherClass extends AClass {}

class NotMyClass extends AClass {}

new MyClass();
new MyOtherClass();
new NotMyClass();


--
Marco Kaiser

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux