Derived Class Inheriting Public Method Which Returns Private Value of Super Class

Started by
2 comments, last by Trienco 12 years, 9 months ago
So the title is a whopper, but it basically explains the just of what my problem is. I have a super class with a private value that I do not want to be inherited in derived classes (and since it's private, it doesn't get inherited). The problem is, in this super class I also have a public function (a getter) which returns that private value. Now, if the user creates a new instance of the derived class and invokes that inherited public getter function, it doesn't return the private value (as it shouldn't), but i want it to actually throw an error saying the function doesn't exist (since, in actuality, a function shouldn't exist if it returns a value that the derived class doesn't have access to.)

Any ideas on how to do this? I've thought about overloading the function in the derived class and just having it die() with an error or something, but that seems inefficient. Here's some sample code to reiterate what I just said:


<?php

// super class
class A {

public function getPrivateField() {
return $this->mPrivateField;
}

private $mPrivateField;
}

// derived class
class B extends A {
// stuff goes here...
}

// example of a valid call
$a = new A();
$a->getPrivateField(); // returns $mPrivateField

// example of what "should be" an invalid call altogether
$b = new B();
$b->getPrivateField(); // returns garbage, i assume. i know it doesn't return $mPrivateField
// but i want it to throw a call error saying the function doesn't exist

?>
Advertisement
if you make it "protected" in your super-class it will be private in the sub-class and won't be accessable from outside.

but why should the sub-class does not have access to this member ? sounds like a strange design...

Now, if the user creates a new instance of the derived class and invokes that inherited public getter function, it doesn't return the private value (as it shouldn't), but i want it to actually throw an error saying the function doesn't exist (since, in actuality, a function shouldn't exist if it returns a value that the derived class doesn't have access to.)

Everything you said here is completely wrong. When you inherit from a class, you're declaring that the derived class IS the class it's deriving from. That means it shares a contract with that superclass, even though it may add additional methods.

What are you using inheritance for in this example? It might be easier to illustrate why your design is problematic if you provide specifics.


Now, if the user creates a new instance of the derived class and invokes that inherited public getter function, it doesn't return the private value (as it shouldn't), but i want it to actually throw an error saying the function doesn't exist (since, in actuality, a function shouldn't exist if it returns a value that the derived class doesn't have access to.)


You just made the very common practice of public getter/setter methods for private members completely pointless by saying they shouldn't exist. What do you consider the difference between a derived class calling this function and a complete "stranger" calling it? Why on earth would a "stranger" be allowed to call a function that your own "offspring" shouldn't even know about?

Also, if your derived getter function does not return the correct (private) value that smells like a big bug, not "as it should be".
f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement