php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28158 Can't declare function protected in interface
Submitted: 2004-04-26 15:44 UTC Modified: 2004-04-26 17:43 UTC
From: dcahh at gmx dot de Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5CVS-2004-04-26 (dev) OS: XP Pro
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dcahh at gmx dot de
New email:
PHP Version: OS:

 

 [2004-04-26 15:44 UTC] dcahh at gmx dot de
Description:
------------
I can't have an abstract class provide it's functions protected to it's child classes, since I'm able to declare only public functions within an interface.

Reproduce code:
---------------
<?php

	interface AnyClassInterface {

		protected function doSomething ($sTest);

	}

	abstract class AnyClassAbstract {

		protected function doSomething ($sTest) {

			echo $sTest;

		}

	}

	class AnyClass {

		public funtion __construct() {

			$this->doSomething ('Hello World');

		}

	}

	$oAnyObject = new AnyClass();

?>

Expected result:
----------------
One should be able to declare a function in an interface to be protected, so that classes extending the abstract class, which is implementing the interface is able to provide it's functions only to child-classes and to protect them from foreign classes.

Actual result:
--------------
Fatal error: Access type for interface method AnyClassInterface::doSomething() must be omitted or declared public in D:\apache-webroot\test\testphp\php5.php on line 5

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-26 17:34 UTC] wez@php.net
interfaces are, by definition, public.
Abstract classes are not the same as interfaces.
 [2004-04-26 17:43 UTC] dcahh at gmx dot de
Yes, but is it correct that one can not declare a protected function in an abstract class as long as same implements an interface?
 [2004-07-29 04:08 UTC] ah08010-zend at yahoo dot com
Interfaces need, at a minimum, the ability to regulate the default public "interface" of their implementors.

This includes specifically UNpublicizing the "default" public methods:  __(con|de)struct, __call, __get, __set, etc.

Even if the interface vs. abstract class is valid, it should still be possible to mark __construct as private for e.g., Singleton behavior.

=Austin
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 17:01:34 2025 UTC