PHP: How to call function of a child class from parent class
That's what abstract classes are for. An abstract class basically says: Whoever is inheriting from me, must have this function (or these functions).
abstract class whale
{
function __construct()
{
// some code here
}
function myfunc()
{
$this->test();
}
abstract function test();
}
class fish extends whale
{
function __construct()
{
parent::__construct();
}
function test()
{
echo "So you managed to call me !!";
}
}
$fish = new fish();
$fish->test();
$fish->myfunc();
Call child method from parent class
You can do this by just simply calling the child method, e.g.:
if($_POST['condition'] === 'A') :
$this->some_parent_function();
$this->child_1_action();
However, you should avoid doing this. Putting checks in the parent that call methods only existing in a child class is a very bad design smell. There is always a way to do things in a more structured manner by utilizing well-known design patterns or simply thinking the class hierarchy through better.
A very simple solution you can consider is implementing all of these methods in the parent class as no-ops; each child class can override (and provide implementation for) the method that it's interested in. This is a somewhat mechanical solution so there's no way to know if it's indeed the best approach in your case, but even so it's much better than cold-calling methods that technically are not guaranteed to exist.
Calling a child method from the parent class in PHP
Your idea of inheritence is correct, just not the visibility.
Protected can be used by the class and inherited and parent classes, private can only be used in the actual class it was defined.
php call child class function from parent
Within your child class call the parents constructor
class SubClass extends BaseClass {
function __construct() {
parent::__construct();
print "In SubClass constructor\n";
}
}
PHP call overridden child class function from parent class function in a child object
class B -> ab() called;
<?php
class A
{
function aa(){
//do something in aa
$this->ab();
}
function ab(){
//do something in ab
echo "class A\n";
}
}
Class B extends A {
function ab(){
//do something else in ab
echo "class B\n";
}
}
$b = new B();
$b->aa();
How do I access a parent's methods inside a child's constructor in PHP?
Use parent
as predefined reference: parent::run()
. This will ensure you call parent method. The same way you could call first parent constructor first or after child one - parent::__construct()
.
Class Child extends Parent {
public function __construct() {
parent::__construct();
// Access parent methods here?
$some_arg = NULL; // init from constructor argument or somewhere else
parent::run($some_arg); // explicitly call parent method
// $this->run($some_arg); // implicitly will call parent if no child override
}
}
If you dont have an implementation in child you could call $this->run($args)
, where it will again call parent run method.
Related Topics
Php.Ini Is Nonexistent Loaded Configuration File (None)
How to Protect from Downloading a Video from a Site
Get the Sum of All Digits in a Numeric String
How to Alias a Table in Laravel Eloquent Queries (Or Using Query Builder)
Split Camelcase Word into Words with PHP Preg_Match (Regular Expression)
Destroy or Unset Session When User Close the Browser Without Clicking on Logout
Is It Okay to Use Array[Key] in PHP
How to Use Objects from Other Namespaces and How to Import Namespaces in PHP
Executing a Shell Script in Background with PHP
Exception: Serialization of 'Closure' Is Not Allowed
Executing Multiple SQL Queries in One Statement with PHP
What Does This Symbol Mean in PHP <=
Php7.1 JSON_Encode() Float Issue
Php: How to Call Function of a Child Class from Parent Class