Understanding Classes in PHP

I am officially mentally retarded. [Let me explain]

I have never given lessons and their relationship until today. I'm trying to understand something that seems pretty obvious, but since I'm stupid, I don't see it.

Let's say I have a base class that will be extended from different files. How classes of children can invoke functions from other siblings (that is, if they are considered children at all). Code example: (don't kill me)

class cOne { 
    public function functionOne(){
       echo "something";
    }
}

Then in another file I say:

class cOneChildOne extends cOne { 
    public function functionOne(){
       echo "something from child one";
    }
}

And in another file I say:

class cOneChildTwo extends cOne { 
    public function functionOne(){
       echo "something from child two";
    }
}

How can I create a new object, and when I can access the functions from both the children and the parent class in a similar style $newObject->Children->function();, I seriously consider the option, today I lost my brain and I can’t think straight.

, , - , : $newObject = new cOne; , -, .

- , . , , .

!!

+3
9

class C1{  
  static public $childs=array();
  function __construct(){
    self::$childs[]=$this;    
  }
}
class C1C1 extends C1{
  function hi(){
    echo 'hi from C1C1 <br />';
  }  
}
class C1C2 extends C1{
  function hi(){
    echo 'hi from C1C2 <br />';
  }    
}

$c1 = new C1C2();
$c2 = new C1C2();
$c3 = new C1C1();
$c4 = new C1C1();
$c5 = new C1C1();
$c6 = new C1C1();
foreach(C1::$childs as $c){
  $c->hi();
}
+2

cOne , php, , parent::someFunction(), . , cOne , cOne, , cOne , .

+1

parent::functionOne(); . .

0

!

!!!= O) = O) = )

, s methods and method . , , .

0

kgb, , "" . cOneChildOne, -, "- " (*). , parent::functionOne(), cOne , , "-\nsomething " - .

(*) . , :

public function functionOne(){
   return "something from child one";
}

, ..

0

cOneChildTwo cOneChildOne , . $this ( parent:: __ construct()), cOneChildTwo .

, . , .

0

.

- . , , .

, child1 child2, - . child2 , child1 .

0

, , . , , , - . / - , , , , . . , , , .

 child1.communicateWithAnotherChild(someOtherChild)
0

Why don't you have static Array $childrena class cOne, and the methods __construct()for the classes that extend it are simply called cOne::addChild($this)? Each of them can implement a function getChildrenthat sends a request to a superclass - should it not work?

0
source

Source: https://habr.com/ru/post/1751907/


All Articles