Re: [PHP] interfaces - your opinion

2005-12-30 Thread Jochem Maas

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



Re: [PHP] interfaces - your opinion

2005-12-30 Thread Marco Kaiser
Hi Jochem,


i know what you mean, but you can't define code in interfaces to implement a
logic. You should use an abstract class to restrict the access. I think a
interface should just define a rude prototype of function 

--
Marco Kaiser


Re: [PHP] interfaces - your opinion

2005-12-30 Thread Richard Lynch
On Thu, December 29, 2005 10:31 am, Jochem Maas wrote:
 anyone here work with Interfaces? I do and I like them alot;

 I recently ran into a situation where it would be nice to be able to
 specify
 that a given interface is only allowed to be implemented by a certain
 class (and all subclasses thereof) - Anyone have an idea as to how
 to implement such a restraint in a clean way? does anyone think that
 this
 is generally useful?

 maybe it's something that should not be done at all.
 maybe the implementation belongs in the engine (with some new syntax)
 e.g:

   class MyClass {}
   class MySubClass extends MyClass {}
   class MyOtherClass {}
   class SomebodyElsesClass {}

   interface MyIface validfor MyClass, MyOtherClass
   {
   abstract function blabla();
   }

   // SomebodyElsesClass would not be able to implement MyIface


 interested to know if anyone has an opinion on this.

I don't use Interfaces and all that stuff, but...

Seems to me that every explanation I've ever heard for using
Interfaces in the first place was to provide an abstract definition of
what a class was supposed to implement, and that any class that
implemented the required functionality should, in some theoretical
la-la land, be a suitable replacement for any other class.

Seems to me that if you restrict the implementation to a specific
branch of classes, you're kinda defeating the whole purpose of an
Interface, based on my rudimentary understanding of what an Interface
is supposed to be.

I'm sure it makes perfect sense in the context you need, but...

Makes me think maybe an Interface isn't what you ought to be using in
the first place...

-- 
Like Music?
http://l-i-e.com/artists.htm

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



[PHP] interfaces - your opinion

2005-12-29 Thread Jochem Maas

anyone here work with Interfaces? I do and I like them alot;

I recently ran into a situation where it would be nice to be able to specify
that a given interface is only allowed to be implemented by a certain
class (and all subclasses thereof) - Anyone have an idea as to how
to implement such a restraint in a clean way? does anyone think that this
is generally useful?

maybe it's something that should not be done at all.
maybe the implementation belongs in the engine (with some new syntax) e.g:

class MyClass {}
class MySubClass extends MyClass {}
class MyOtherClass {}
class SomebodyElsesClass {}

interface MyIface validfor MyClass, MyOtherClass
{
abstract function blabla();
}

// SomebodyElsesClass would not be able to implement MyIface


interested to know if anyone has an opinion on this.

rgds,
Jochem

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