I have model
BaseUser.class.php
User.class.php
UserTable.class.php
In user Class I have been override the delete function
Technically this is not possible from the "outside" (the public interface) for good reason.
If you have understood why (otherwise read below), and you actually know what you do, there is no reason to actually not offer the functionality:
class User extends BaseUser {
...
function parentDelete(){
parent::delete();
}
...
}
user = new User();
$user->delete(); // will call the overridden delete
$user->parentDelete(); // want to call parent delete
However if you ever do that, you should know that you have misused inheritance somehow. This must be an exceptional situation as I can not imagine any situation where you actually need that to do at all.
So try to formulate why you need that functionality so to give yourself better suggestions.
Why is that bad?
For a very simple reason: In your software you do not need to know that $user
has a parent or not. That is some detail you should not care at all about.
That will allow you to replace any user-object you have in your software with a childobject of user later on. This is important as you want to change your software over time.
If you make the internal detail part of the public interface you are robbing yourself the possibility to keep things flexible. Not being flexible is really a bad situation.
I think you are looking for the PHP parent functions:
<?php
class A {
function example() {
echo "I am A::example() and provide basic functionality.<br />\n";
}
}
class B extends A {
function example() {
echo "I am B::example() and provide additional functionality.<br />\n";
parent::example();
}
}
$b = new B;
// This will call B::example(), which will in turn call A::example().
$b->example();
?>
If your class doesn't have a function called getMyFunction() calling $b->getMyFuction()
will call the parent function. If it does, it will call the child function, unless the child function calls the prent function.
maybe try this
parent::delete();
Why do you need to extend delete
if you need to call the parent one time?
Why not create a custom delete?
Like:
public function customDelete()
{
parent::delete();
// do extends delete here
}
Then you can delete an object using ->customDelete()
and you also can call the delete method from the parent since you didn't override the delete()
method:
$user = new User();
$user->customDelete(); // will call the overridden delete
$user->delete(); // will call the parent (since you didn't override it)
And if you really need to override the delete()
function, you still can create a kind of parent delete:
public function parentDelete()
{
return parent::delete();
}
Then:
$user = new User();
$user->delete(); // will call the overridden delete
$user->parentDelete(); // will call the method that only call the parent
It can be done using call_user_func
. Yes, its bad practice, but its possible.
class BaseUser {
public function delete()
{
echo 'BaseUser deleted';
}
}
class User extends BaseUser{
public function delete()
{
echo 'User deleted';
}
}
$instance = new User();
call_user_func(array($instance, 'parent::delete'));
result:
BaseUser deleted
I found this thread on codingforums. What it says (and I tend to agree with them) is that if you have an overridden function/method in a child class, an instance of that object will ALWAYS call the child method.
The parent
keyword is akin to $this
or self
and can only be called from within the class itself.
If you want a child object to be able to call the parent method, just create another method
function parentExample() {
parent::example();
}
which only calls the parent method.