Php super function call

PHP Inheritance

Summary: in this tutorial, you will learn about the PHP inheritance that allows a class to reuse the code from another class.

Introduction to the PHP inheritance

Inheritance allows a class to reuse the code from another class without duplicating it.

In inheritance, you have a parent class with properties and methods, and a child class can use the code from the parent class.

Inheritance allows you to write the code in the parent class and use it in both parent and child classes.

The parent class is also called a base class or super class. And the child class is also known as a derived class or a subclass.

To define a class inherits from another class, you use the extends keyword.

Suppose that you have a class called BankAccount as follows:

 class BankAccount < private $balance; public function getBalance() < return $this->balance; > public function deposit($amount) < if ($amount > 0) < $this->balance += $amount; > return $this; > >Code language: HTML, XML (xml)

The BankAccount class has the private property $balance and the public methods getBalance() and deposit() .

To declare that the SavingAccount inherits from the BankAccount , you use the extends keyword as follows:

 class SavingAccount extends BankAccount  Code language: HTML, XML (xml)

In this example, the SavingAccount can reuse all the non-private properties and methods from the BankAccount class.

The following creates a new instance of the SavingAccount class, calls the deposit() method and shows the balance:

 require 'SavingAccount.php'; $account = new SavingAccount(); $account->deposit(100); echo $account->getBalance(); Code language: HTML, XML (xml)

In this example, the SavingAccount class reuses the properties and methods from the BankAccount class.

A child class can reuse properties and methods from the parent class. But the parent class cannot use properties and methods from the child class.

Add properties and methods to the child class

A child class can have its own properties and methods. In the following example, we add the $interestRate property and setInterestRate() method to the SavingAccount class:

 class SavingAccount extends BankAccount < private $interestRate; public function setInterestRate($interestRate) < $this->interestRate = $interestRate; > >Code language: HTML, XML (xml)

Call methods from the parent class

To call a method from the parent class, you use the $this keyword.

The following example defines the addInterest() method that calculates the interest and adds it to the balance:

 class SavingAccount extends BankAccount < private $interestRate; public function setInterestRate($interestRate) < $this->interestRate = $interestRate; > public function addInterest() < // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); > >Code language: HTML, XML (xml)

In the addInterest() method, we call the getBalance() method from the BankAccount class for calculating the interest and the deposit() method to add the interest to the balance.

 class BankAccount < private $balance; public function getBalance() < return $this->balance; > public function deposit($amount) < if ($amount > 0) < $this->balance += $amount; > return $this; > > class SavingAccount extends BankAccount < private $interestRate; public function setInterestRate($interestRate) < $this->interestRate = $interestRate; > public function addInterest() < // calculate interest $interest = $this->interestRate * $this->getBalance(); // deposit interest to the balance $this->deposit($interest); > > $account = new SavingAccount(); $account->deposit(100); // set interest rate $account->setInterestRate(0.05); $account->addInterest(); echo $account->getBalance(); Code language: HTML, XML (xml)

Inhertiance and UML

You use the open arrow from the child class to the parent class to represent the inheritance relationship. The following UML diagram shows the inheritance relationship between the BankAccount and SavingAccount classes:

Summary

  • Inheritance allows a class to reuse the code of another class without duplicating it.
  • Use the extends keyword to define one class that inherits from another class.
  • A class that inherits another class is called a subclass, a child class, or a derived class. The class from which the subclass inherits is a parent class, a superclass, or a base class.
  • A subclass can have its own properties and methods.
  • Use $this keyword to call the methods of the parent class from methods in the child class.

Источник

PHP call super parent method

Result: Changing «extends CI_Driver» to «extends Prueba» in Child class, and removing the «_parent» in the variable value, in child constructor to: returns this error: Adding in child constructor, then trying to get the value from parent method or directly the variable. I need to access a method of the parent class as a global variable in a child class.

PHP call super parent method

I’m not sure at all if there is such a term but here is what I want to do

class A < function a($a) < return $a; >> class B extends A < function a($a) < return parent::a('B' . $a); >> class C extends B < function a($a) < return superparent::a('C' . $a); >> $c = new C(); echo $c->a('C'); // "CC" 

I want to bypass all medial classes and call the function on the parent where it was defined for the first time — is this possible?

No, and it’s a terrible idea. All your class C knows is that it extends B . It cannot know nor should it know what, if anything, B extends. C cannot count on B extending anything, nor should it be aware of specific methods of that grand parent and their implementation details. You may be refactoring B tomorrow to extend another class, or to extend no class at all. Code would start breaking left and right if you established such three-class-crossover dependencies.

Classes should only interact with peers they’re directly associated with, either through inheritance or dependency injection.

Just logically speaking: B adds something necessary to the behaviour of A to «make it a B «, and C adds something to make it a C . C builds on B , so anything B does C should do as well. Having C «jump over» B back to A suggests that your logic and responsibility assignment is mixed up in your class hierarchy.

Despite of any Engineering Software complaint (what you are trying to do is something that must be achieved by some other approach) I thought that both my examples are suposed to work:

Using a trait:

 > class A < function a($a) < return $a; >function super_ref() < return $this; >> class B extends A < use Bypass_trait; function a($a) < return $this->super_ref()->a('B' . $a); > > class C extends B < use Bypass_trait; function a($a) < return $this->super_ref()->a('C' . $a); > > $c = new C(); echo $c->a('C'); // "CC" 

Using an interface:

 class A implements Bypass < function a($a) < return $a; >function super_ref() < return $this; >> class B extends A implements Bypass < function a($a) < return $this->super_ref()->a('B' . $a); > function super_ref() < return parent::super_ref(); >> class C extends B implements Bypass < function a($a) < return $this->super_ref()->a('C' . $a); > function super_ref() < return parent::super_ref(); >> $c = new C(); echo $c->a('C'); // "CC" 

i thing you can make «A» as abstract, then let b and c override it

You can choose which parent method you call with A::a(‘C’ . $a) .

When calling a parent function like this, it does not call it statically, just like parent::a() .

How do I access a parent’s methods inside a child’s constructor in, Use parent as predefined reference: parent::run() . This will ensure you call parent method. The same way you could call first parent

How do I access a parent’s methods inside a child’s constructor in PHP?

Say I have class child() and class parent() . The parent has a constructor and a few other public methods, and the child is empty apart from a constructor.

How do I go about calling a parent’s methods inside of the child’s constructor, as in:

Class Parent < public function __construct() < // Do stuff (set up a db connection, for example) >public function run($someArgument) < // Manipulation return $modifiedArgument; >> Class Child extends Parent < public function __construct() < // Access parent methods here? >> 

Say I want to call parent s run() method, do I have to call a new instance of the parent inside the child constructor? Like so.

$var = new Parent(); $var->run($someArgument); 

If so, what is the point of extends from a class definition POV? I can call a new instance of another class with the new keyword whether it extends the ‘child’ or not.

My (likely) wrong understanding was that by using extends you can link classes and methods from a parent can be inherited into the child. Is that only outside the class definition? Does using extend offer no efficiencies inside the class definition?

Because referring to the parent’s run() method with the this keyword certainly doesn’t work.

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.

function a() < echo 'I exist everywhere'; >class A < protected $a function a() < $this->a = 'I have been called'; > function out() < echo $this->a; a(); > > class B extends A < function __construct() < parent::a();// original method $this->a(); // overridden method a(); > function a() < $this->a = $this->a ? 'I have been overwritten' : 'first call'; > > 

Study these to understand the difference

Access child class variable with parent method, This is bad OOP practice. Your parent class cannot be used directly if it depends on data from subclasses. The problem you are trying to

Codeigniter driver call parent method in child construct

I have a problem implementing a Driver in Codeigniter.

I need to access a method of the parent class as a global variable in a child class.

valid_drivers = array('uno', 'dos'); $this->notifica_grupo = "foo"; > public function notifica_grupo() < return $this->notifica_grupo; > > 
ci =& get_instance(); $this->notifica = $this->_parent->notifica_grupo(); > public function test1()< echo $this->notifica; > > 

I get the following error when calling $this->prueba->uno->test1();

 Type: Error Message: Call to a member function notifica_grupo() on null Filename: /xxx/application/libraries/Prueba/drivers/Prueba_uno.php Line Number: 10 Backtrace: File: /xxx/application/controllers/Test.php Line: 33 Function: __get 

So, my question is. Is there a way to access parent method from child’s construct?

UPDATE : I tried different ideas, none worked. These have been the scenarios:

  1. Trying to get the variable directly instead to get from the parent’s method: $this->notifica = $this->_parent->notifica_grupo; . Result:
Message: Trying to get property 'notifica_grupo' of non-object 
Invalid driver requested: Prueba_uno__parent 
  1. Changing «extends CI_Driver» to «extends Prueba» in Child class, and removing the «_parent» in the variable value, in child constructor to: $this->notifica = $this->notifica_grupo(); returns this error:
Message: Call to undefined method Prueba_uno::decorate() 
  1. Adding parent::__construct(); in child constructor, then trying to get the value from parent method or directly the variable. Returns:
Type: Error Message: Cannot call constructor 

You are not extending the parent class. Your child class must use the parent class name to extend which is Prueba. And you are extending CI_Driver

You need to use parent class for this you can do this $this->notifica = Prueba ::notifica_grupo();

Can I call inherited parent method without a name in PHP?, I find it more natural to just «inherit()» it, like you do in many other languages. It’s also a bit easier when you change the method name, you

Источник

How to call super in PHP?

In both classA and classB I define the method fooBar() .

In fooBar() of classB I want to call fooBar() of classA at the beginning.

Just the way I’m used to, from Objective-C. Is that possible in PHP? And if so, how?

openfrog Avatar

asked Dec 26 ’09 03:12

openfrog

People also ask

Super is called when we want to inherit some of the parameters from the parent class. It is not compulsory as the compiler always call the default constructor.

The super keyword is used to call the constructor of its parent class to access the parent’s properties and methods. Tip: To understand the «inheritance» concept (parent and child classes) better, read our JavaScript Classes Tutorial.

Inheritance allows you to write the code in the parent class and use it in both parent and child classes. The parent class is also called a base class or super class. And the child class is also known as a derived class or a subclass. To define a class inherits from another class, you use the extends keyword.

2 Answers

The . double colon, is a token that allows access to . overridden properties or methods of a class.

.

Example #3 Calling a parent’s method

 > class OtherClass extends MyClass < // Override parent's definition public function myFunc() < // But still call the parent function parent::myFunc(); echo "OtherClass::myFunc()\n"; >> $class = new OtherClass(); $class->myFunc(); ?> 

just somebody

Just a quick note because this doesn’t come up as easy on Google searches, and this is well documented in php docs if you can find it. If you have a subclass that needs to call the superclass’s constructor, you can call it with:

parent::__construct(); // since PHP5 

An example would be if the super class has some arguments in it’s constructor and it’s implementing classes needs to call that:

class Foo < public function __construct($lol, $cat) < // Do stuff specific for Foo >> class Bar extends Foo < public function __construct()( parent::__construct("lol", "cat"); // Do stuff specific for Bar >> 

You can find a more motivating example here.

Источник

Читайте также:  Df isna sum python
Оцените статью