radalin Posted November 24, 2008 Share Posted November 24, 2008 Hi, I'm trying to create an interface with private methods. But when I try to run the file, I get an error: Access type for interface method myInterface::prvfnc() must be omitted in c:\htdocs\file.php on line 12 My interface is like this: interface myInterface { private function prvfnc(); } and my class is like: class myClass implements myInterface { private function prvfnc() { echo "private function call"; } } Is private methods are not allowed in PHP interfaces? Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/ Share on other sites More sharing options...
rhodesa Posted November 24, 2008 Share Posted November 24, 2008 i don't think interfaces define if a method is public/protected/private. that is something that the class implementing it does. Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697783 Share on other sites More sharing options...
trq Posted November 24, 2008 Share Posted November 24, 2008 That should be fine. What php version are you using? Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697787 Share on other sites More sharing options...
radalin Posted November 24, 2008 Author Share Posted November 24, 2008 The problem is, if I do not define public/private/protected in the interface, it takes the access level of the method as public and in class if I define it as a private function I get an error like this: Access level to myClass::prvfnc() must be public (as in class myInterface) in C:\htdocs\file.php on line 12 Is it a bug perhaps? Edit: PHP version is 5.2.6 Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697788 Share on other sites More sharing options...
trq Posted November 24, 2008 Share Posted November 24, 2008 Which is line 12? Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697790 Share on other sites More sharing options...
448191 Posted November 24, 2008 Share Posted November 24, 2008 All methods in an interface must be public. It wouldn't much of an interface otherwise, would it (stop and think for a sec)? Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697793 Share on other sites More sharing options...
radalin Posted November 24, 2008 Author Share Posted November 24, 2008 Well, honestly that really makes sense Why to define a function which you can never be able to call... But how I'm using the interface is to define which functions must have a class and it looks like more of a blue print than a real interface. But you are quite right about that Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697797 Share on other sites More sharing options...
448191 Posted November 24, 2008 Share Posted November 24, 2008 If you want to ensure child classes implement a common method, but not expose it outside the hierarchy, use an abstract protected method. Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697806 Share on other sites More sharing options...
radalin Posted November 24, 2008 Author Share Posted November 24, 2008 Yes I guess that was the way to use it. Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697808 Share on other sites More sharing options...
trq Posted November 24, 2008 Share Posted November 24, 2008 All methods in an interface must be public. It wouldn't much of an interface otherwise, would it (stop and think for a sec)? Hehe, I didn't even think about it that way. Still, didn't think it would throw an error either. Quote Link to comment https://forums.phpfreaks.com/topic/134051-solved-interface-with-private-methods/#findComment-697809 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.