Protected methods in interfaces

php.internals

Ferdinand Beyer

22 years ago
Trying to write a singleton interface: interface Singleton { // Disallow public construction protected function __construct(); public static function getInstance(); } I got an error saying I was not allowed to declare the constructor protected - I should either use public or omit it. Why can't I instruct Singleton-classes to use protected constructors? Is this expected behavior or may I file a bug report?
-- Ferdinand Beyer <fb@fbeyer.com>

Marcus Börger

22 years ago
Hello Ferdinand, Thursday, August 5, 2004, 5:01:42 PM, you wrote:
> Trying to write a singleton interface:
> interface Singleton { > // Disallow public construction > protected function __construct();
> public static function getInstance(); > }
> I got an error saying I was not allowed to declare the constructor > protected - I should either use public or omit it.
> Why can't I instruct Singleton-classes to use protected constructors? > Is this expected behavior or may I file a bug report?
All interface methods must be public - that's the point behind interfaces. regards marcus

Jason Garber

22 years ago
Hello Marcus, Can abstract methods be protected?
-- Best regards, Jason mailto:jason@ionzoft.com Thursday, August 5, 2004, 1:32:05 PM, you wrote: MB> Hello Ferdinand, MB> Thursday, August 5, 2004, 5:01:42 PM, you wrote: >> Trying to write a singleton interface: >> interface Singleton { >> // Disallow public construction >> protected function __construct(); >> public static function getInstance(); >> } >> I got an error saying I was not allowed to declare the constructor >> protected - I should either use public or omit it. >> Why can't I instruct Singleton-classes to use protected constructors? >> Is this expected behavior or may I file a bug report? MB> All interface methods must be public - that's the point behind interfaces. MB> regards MB> marcus

George Schlossnagle

22 years ago
On Aug 5, 2004, at 1:51 PM, Jason Garber wrote:
> Hello Marcus, > > Can abstract methods be protected?
No. George

Ferdinand Beyer

22 years ago
On 5 Aug 2004 at 13:59, George Schlossnagle wrote:
> > Can abstract methods be protected? > > No.
Why not? Look at my singleton example...
-- Ferdinand Beyer <fb@fbeyer.com>

Marcus Börger

22 years ago
Hello Ferdinand, Thursday, August 5, 2004, 8:07:05 PM, you wrote:
> On 5 Aug 2004 at 13:59, George Schlossnagle wrote:
>> > Can abstract methods be protected? >> >> No.
> Why not? Look at my singleton example...
Even your protected __construct can be overwritten and made public again. So you'd need a private __construct in your interface. Also the pattern singleton requires at least one static proected or private member variable and a little bit code in function getInstance(). Having said this the only way to do what you are trying to do is using templates in c++ or compareable elements of other languages fully supporting functional programming. Having saif this and looking back at what you can do is defining the access interface to your singletons. Thus the interface would reduce to: interface Singleton { function getInterface(); } So as usually with interfaces you have to fully implement the functionality behind that interface every time it is realized. Best regards, Marcus