Tony Ford wrote:
I understand it make no sense to change a private member to protected or a protected member to public, but what about the other way around? Example:

class Foo
{
public $my_prop = '';
}

class Bar extends Foo
{
protected $my_prop = 'bleh';
}

Why can't I do this in php? I can't think of a reason you "should not" be allowed to do this.

Subclassing is about the relationship between common structure of parent classes and their subclasses. A class is a common part of its subclasses. If class A has public elements a, b, and c, then all subclasses of A must have those public elements in common.

If one of the subclasses can "take away" methods or variables, removing them from public visibility by making them protected, then it does not have the same common set of public elements as other subclasses, and ceases to satisfy the common-structure relationship with its parent or sibling classes. In a way, it ceases to have an "is a" relationship with its parent.

If you could hide some elements of the public interface, what's to stop you from hiding _all_ of the elements of the public interface? If you do that, how could you even claim that the class is derived from its parent anymore?

Bill Karwin

Reply via email to