Sign in to follow this  
Master Jake

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

Recommended Posts

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:

[code]
<?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

?>
[/code]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
[quote name='Master Jake' timestamp='1310363409' post='4833595']
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.)[/quote]
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.


Share this post


Link to post
Share on other sites
[quote name='Master Jake' timestamp='1310363409' post='4833595']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.)
[/quote]

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".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this