Reputation: 109
Is it possible to block the visibility of functions from a parent class in sub classes?
class DB {
function connect() {
// connects to db
}
}
class OtherClass extends DB {
function readData() {
// reads data
}
}
class AnotherOtherClass extends OtherClass {
function updateUser($username) {
// add username
}
}
If I were to write:
$cls1= new OtherClass();
$cls1->connect(); // want to allow this class to show
$cls2= new AnotherOtherClass();
$cls2->connect(); // do not want this class to show
$cls2->readData(); // want to allow this class to show
Is this possible?
Upvotes: 0
Views: 88
Reputation: 164776
Sounds like you don't actually want AnotherOtherClass
to extend OtherClass
. Perhaps you want to consume / wrap / decorate OtherClass
instead, eg
class AnotherOtherClass
{
private $other;
public function __construct(OtherClass $other)
{
$this->other = $other;
}
public function readData()
{
// proxy to OtherClass::readData()
return $this->other->readData();
}
public function updateUser($username)
{
// add username
}
}
You could also do this but it smells bad
class AnotherOtherClass extends OtherClass
{
public function connect()
{
throw new BadMethodCallException('Not available in ' . __CLASS__);
}
Upvotes: 1