Calling a static method from class B (which extends class A) of class A

In the practical test there was an interesting question to which I did not understand the answer. What is the result of the following code:

<?php class Foo { public $name = 'Andrew'; public function getName() { echo $this->name; } } class Bar extends Foo { public $name = 'John'; public function getName() { Foo::getName(); } } $a = new Bar; $a->getName(); ?> 

I initially thought this caused an error because static methods cannot reference $ this (at least PHP5). I tested this myself, and in fact it outputs John.

I added Foo :: getName (); at the end of the script and got the error I was expecting. So, what changes when you call a static method from a class that extends the class you are calling from?

Could someone explain in detail what is happening here?

+5
oop php
source share
5 answers

$ this to the object in the context of which the method was called. So: $ is $ a-> getName () - $ a. $ this in $ fooInstance-> getName () will be $ fooInstance. In the case when $ this is given (in a $ object, a method call) and we call a static method, $ this remains assigned to $ a.

It seems like confusion may arise from this function. :)

+2
source share

Foo :: getName () uses an older style PHP4 scope statement to allow calling an overridden method.

In PHP5, you should use parent :: getName () instead

This is useful if you want to extend rather than completely override the behavior of the base class, for example. it can make it clearer

 class Bar extends Foo { public $name = 'John'; public function getName() { echo "My name is "; parent::getName(); } } 
+7
source share

If you call a static method associated with another object, the method executes in the context of the current object. This allows access to the $ this object.

The best way to call a superclass method from within a subclass would be:

 parent::getName(); 
+4
source share

When you call $a->getName() , you are referencing a specific object, $a , which has the class Bar and therefore returns "John".

Foo::getName() not valid outside the function because there is no specific object.

I'm not sure if it works in PHP, but if you put the object in a superclass, like in (Foo)$a->getName() , then you will get "Andrew" as your result. You will still be talking about a specific object ( $a ), but in this case, like Foo . (Note that you don’t want to do this at all)

+1
source share

Sometimes programmers are better versed in code than in English.

The first thing that happens here is the concept of overload. When you create an instance of Bar, the getName () method overloads the method with the same name in Foo.

Overloading is an important and important part of OOD.

However, it is often useful to be able to call a version of a method that exists in the Parent (Foo) class.

Here is an example:

 class Dog { public function getTag() { return "I'm a dog."; } } class Skip extends dog { public function getTag() { return Dog::getTag() . " My name is Skip."; // I'm using Dog:: because it matches your example. However, you should use parent:: instead. } } $o = new Skip(); echo $o->getTag(); // Echo's: "I'm a dog. My name is Skip." 

Clearly, this is a very narrow example, but it illustrates the point.

Your base class is the most common type implementation. In this case, it is "Dog." You want to put information in this base class, which is common to all instances of this type. This prevents duplication in each of the Derived classes (for example, "Skip").

Your script uses this function, perhaps inadvertently.

+1
source share

All Articles