i am a new PHP, i have some files like this:
In root.php i have
<?php
class Root
{
// Some functions here
}
In child_1.php i have:
class Child_1 extends Root
{
function abc()
{
//Some code here
}
}
In child_2.php i have:
class Child_2 extends Root
{
function cde()
{
// How can i call function abc() from class Child_1 here
}
}
Yes, how can i call a function of Child_1 class from a function of Child_2 class.
As classes that extend one parent (Root) know nothing about each other (and you cannot change this), you can do:
class Chirld_2 extends Root
{
function cde()
{
$obj = new Chirld_1();
$obj->abc();
}
}
But obviously it's a lack of good design. I suppose, you have to define abc() in a Root class to make it available in both children:
class Root
{
function abc()
{
//Some code here
}
}
class Chirld_1 extends Root
{
}
class Chirld_2 extends Root
{
}
$c1 = new Chirld_1();
$c1->abc();
$c2 = new Chirld_2();
$c2->abc();
you can call any function in class_1 or Class_2 of parent class
but within any child class you can't call method from another.because They do not have a relevance
a solution is you call class_2 in constructor of class_1
If you want to use functions of Chirld_1 from Chirld_2 or vice versa without creating an object of the child class, then one option is to declare Chirld_1 and Chirld_2 as traits of the parent class Root. This will allow you to call functions of Chirld_1 from Chirld_2 and vice verse using the $this keyword. The classes should be declared as follows:
<?php
trait Chirld_1
{
public function test1()
{
$this->test2();
}
}
trait Chirld_2
{
public function test2()
{
$this->test1();
}
}
class Root
{
use Chirld_1;
use Chirld_2;
// Some functions here
}
See this links for more information about traits: http://php.net/manual/en/language.oop5.traits.php
Try using parent:: or static:: or self:: notation to designate the class you want to use.
http://php.net/manual/pl/keyword.parent.php
Related
Overwrite namespace usage in extended class
Is it possible to overwrite the used namespace of the parent class without rewriting the function in the extended class?
For clarification i write down an example:
i have two classes like this:
namespace one;
class hey
{
public static function say()
{
echo "hey";
}
}
and
namespace two;
class hey
{
public static function say()
{
echo "ho";
}
}
Now i use on of the namespaces in this class:
use one\hey;
class saysomething
{
public static function main()
{
hey::say();
}
}
Now i want to extend the last class:
class extended extends saysomething
{
}
extended::main();
In this class i want to use namespace "two\one" without overwriting the function, is it possible? *f it is, how?
Thank you for your time.
You must define extended::main() within extended class, like this:
use two\hey;
class extended extends saysomething
{
public static function main()
{
hey::say(); // will call two/hey::say method
}
}
Namespaces won't help here.
In php I have a ROOT class from which all other classes inherit.
abstract class ROOT{
public static function getClass(){
}
}
I want that function to return the class(name) of the object which inherits from this class. So if I create an object Tree (extends ROOT) and call getClass on it it should say "Tree"
The function get_class() only works on objects, so can't be used inside a static function.
Is there any way to accomplish this?
Instead of get_class(), use get_called_class().
http://www.php.net/manual/en/function.get-called-class.php
abstract class ROOT {
public static function getClass() {
return get_called_class();
}
}
class Tree extends ROOT {
}
$Tree = new Tree();
echo $Tree->getClass(); // Outputs "Tree"
If I have an abstract class like this:
abstract class MyApp
{
public function init()
{
$this->stuff = $this->getStuff();
}
public function getStuff()
{
return new BlueStuff();
}
}
And then I have a class that extends from this abstract class like this:
class MyExtendedClass extends MyApp
{
public function init()
{
parent::init();
}
public function getStuff()
{
return new RedStuff();
}
}
If I do:
$myobj = new MyExtendedClass();
$myobj->init();
Why does the method getStuff from the child class get called? Isn't $this in the context of the abstract class? If so, shouldn't the method of the abstract class get called?
Thanks!
New answer
In PHP you can use subclasses as if all methods in the parent class that don't exist in the subclass have been copied to the subclass. So your example would be the same as:
class MyExtendedClass extends MyApp {
public function init() {
$this->stuff = $this->getStuff();
}
public function getStuff() {
return new RedStuff();
}
}
Just think of the subclass as having all code of the parent class and you're normally all right. There is one exception to this rule: properties. A private property of a class can only be accessed by that class, subclasses can't access the private properties of parent classes. In order to do that you need to change the private property into a protected property.
Original answer
Abstract classes in PHP are just like regular classes with one big difference: they can't get initiated. This means you can't do new AbstractClass();.
Since they work exactly like regular classes for everything else, this also is the case for extending classes. This means that PHP first tries to find the method in the initiated class, and only looks in the abstract classes if it doesn't exist.
So in your example this would mean that the getStuff() method from MyExtendedClass is called. Furthermore, this means you can leave out the init() method in MyExtendedClass.
I mean something like that:
class parentClass {
public function method() {
echo $this->prop;
}
}
class childClass extends parentClass {
public $prop = 5;
}
How can I get a child prop from the parent prop?
Thanks...
Either I don't fully understand what you want or the solution is as trivial as the following code.
class parentClass {
public function method() {
echo $this->prop;
}
}
class childClass extends parentClass {
public $prop = 5;
}
$object = new childClass();
$object->method();
I mean the child class is extending the base class which means it will also inherit all the methods of its parent's class. That makes the whole process of using the parent's class method as simple as calling it from the instance of the child class.
All protected and public members of child classes are visible from within their parent class in PHP, so the example code you provided should work just fine. Quote from the php doc:
Members declared protected can be accessed only within the class
itself and by inherited and parent classes.
But the actual question is: do you really need it?
The proper OO way would be to define a self-contained parent class that expresses something. It should not need to access properties of child classes - this is a so-called code smell. If you really think that you have a case where a similar construct is necessary, you are probably looking for abstract methods, which guarantee that every child class has this property:
abstract class Animal {
public function makeNoise() {
echo $this->getNoiseString();
}
protected abstract function getNoiseString();
}
class Cat extends Animal {
protected function getNoiseString() {
return 'meow';
}
}
//parent
class parentClass {
protected $prop = null;
public function method() {
echo $this->prop;
}
}
//child
class childClass extends parentClass {
protected $prop = 5;
}
Make sure the variable is defined in the parentclass as well. So it will be accessible by the parent.
My Class is independant from another Class.
Inside my Class, a function is doing the same but refined job as a function in another Class. Can I use parent:: function_in_another_class() and get my function join that parent funciton's job flow?
No.
In PHP you can only extend from none or one class. As you write both classes are independent to each other, there is no information where to find the one or the other class.
But what you're looking for is probably this:
class A
{
function myFunction() {}
}
class B
{
private $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function myFunction()
{
$this->a->myFunction();
}
}
If any class method already doing the same thing why would you bother call join it?
You can not do it. If you want the same job flow best way to do is to instantiate the other class and invoke that very same method. Thats why we use OOP.
See the example,
interface Fable()
{
public function f();
}
class OtherClass implements Fable
{
public function f()
{
// job flow
}
}
class MyClass
{
private $fable;
public function __construct(Fable $f)
{
$this->fable = $f;
}
public function method1($args){
return $this->fable->f($args);
}
}
If the current class is a child of another class, yes, you can. parent references to the parent class.
From php.net:
<?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();
?>
The best you can do here is to extend Class B from Class A
Class B extends Class A
But, you can also:
class ClassA {
function do_something($args) {
// Do something
}
}
class ClassB {
function do_something_inclassA($args) {
classA::do_something($args);
}
}
Important: calling classa::do_something(); is a static call, in other words with error reporting E_STRICT you will get a static notice warning because function do_something() is not static function do_something()
Also, calling this function statically (i.e. classa::do_something()) means that class a's function cannot refer to $this within it