oop - PHP class extends not working why and is this how to correctly extend a class? -


Hello, I am trying to understand how inherteince works in PHP using Object Oriented Programming. The main class is the computer, the class successor is the mouse. I am expanding the computer class with the mouse square. I use __ constructs in each class, when I separate the class then I first use the PC type and if there is a mouse after that. Computer returns for some reason? Why is it like this?
  class computer {secure $ type = 'null'; Public function __ composition ($ type) {$ this- & gt; Type = $ type; } Public function computertype () {$ this- & gt; Type = strtoupper ($-this-> type); Return $ this- & gt; type; }} Increases class mouse computer {protected $ hasmouse = 'null'; Public function __ composition ($ meus) {$ this- & gt; Hammaus = $ HMouse; } Public function computermouse () {if ($ this-> hmouis == 'y') {return 'This computer has a mouse'; }}} PC = new computer ('PC', 'Y'); Copy $ PC-> gt; Computertype; Copy $ PC- & gt; ComputerHouse;    

This does not work quite well. If you create a new computer , then you get this. You want to create a new mouse () which is a computer (though it sounds like a strange spell for me).

The parent object (computer) does not know that there is a child in it, the reason is that a parent class can have many children.

Normally you have something like an apple extension, or anything you have on a computer that expands on the computer

  $ A = New apple (); $ B = new del ();   

and both ways are available that are defined in the computer, as well as their own methods

Additionally, when you create __ build If you override a method like, you have to explicitly call the parent constructor (or method) if you want to execute it, for example:

  class computer {function __construct } {Do stuff}} Classes are spread across apple computers {function __construct () {parent :: __ construct (); ... work ...}}   

The only method of class that is performed if this child can not find the method in the classroom, then it is a method for the series Sees. To wit. If computer is a 'moving' class in the class and your child does not do it then it will not execute the move class in parent.

Comments