开发者

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

开发者 https://www.devze.com 2023-02-17 12:43 出处:网络
Hi so I\'m trying to understand how inherteince works in PHP using object oriented programming. The main class is Computer, the class that is inheriting is Mouse. I\'m extedning the Computer class wit

Hi so I'm trying to understand how inherteince works in PHP using object oriented programming. The main class is Computer, the class that is inheriting is Mouse. I'm extedning the Computer class with the mouse class. I use __construct in each class, when I istinate the class I use the pc type first and if it has mouse after. For some reason computer returns null? why is this?

class Computer {
    protected $type = 'null';

    public function __construct($type) {
        $this->type = $type;
    }
    public function computertype() {
        $this->type = strtoupper($this->type);
        return $this->type;
    }

}

class Mouse extends Computer {
    protected $hasmouse = 'null';
    public function __construct($hasmouse){
         $this->hasmouse = $hasmouse;

    }
    public function 开发者_JAVA百科computermouse() {
         if($this->hasmouse == 'Y') {
             return 'This Computer has a mouse';
         }
    }

}

$pc = new Computer('PC', 'Y');
echo $pc->computertype;
echo $pc->computermouse;


That's not how inheritance works.

Inheritance (aka extend) allows you to extend child classes based on the properties and methods of their parent class.

Consider the following for example:

class Peripheral {
  private $connectionType;
  private $isConnected = false;

  function __construct($connectionType) {
    $this->connectionType = $connectionType;
  }

  function getConnectionType() {
    return $this->connectionType;
  }

  function connect() {
    $this->isConnected = true;
  }

  function disconnect() {
    $this->isConnected = false;
  } 

  function isConnected() {
    return $this->isConnected;
  }
}

You may now extend the Peripheral class because a Mouse is a Peripheral.

class Mouse extends Peripheral {
  private $numOfButtons;

  function __construct($connectionType, $numberOfButtons) {
    parent::__construct($connectionType);
    $this->numOfButtons = $numOfButtons;
  }

  function getNumOfButtons() {
    return $this->numOfButtons;
  }

  function leftClick() {
    if($this->isConnected())
      echo "Click!";
  }

  function rightClick() {
    if($this->isConnected())
      echo "RightClick!";
  }
}

Now, because Mouse is a subclass of Peripheral, all the methods defined in Peripheral are accessible in Mouse, but not the other way around...

So while I can do this:

$mouse = new Mouse('USB', 2);
echo $mouse->getConnectionType(); // USB
$mouse->connect();
$mouse->leftClick();

I cannot do the following:

$perip = new Peripheral('USB');
echo $perip->getConnectionType(); // USB
$perip->connect();
$perip->leftClick(); // ERROR: leftClick not defined.


It doesn't work quite like that. If you create a new Computer you get just that. You want to create a new Mouse() which is of the type computer (although that seems like strange inheretance to me).

The parent object (computer) has no idea that it has a child (mouse). This is because a parent class can have multiple children.

Generally you'd have something like a apple extending computer, or dell extending computer then you'd have

$a = new apple();
$b = new dell();

and they'd both have the methods available that are defined in computer, plus their own methods.

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

class computer {
   function __construct() { do stuff }
}

class apple extends computer {
   function __construct() {
        parent::__construct();
        ... do stuff ...
    }
}

Only the method of the class you instantiated gets executed. If it cant find the method in the child class it then looks up the chain for a method. ie. if the computer class has 'movemouse' and your child class doesn't it would execute the movemouse class in the parent.


Instead of:

$pc = new Computer('PC', 'Y');

you want

$mouse = new Mouse('PC', 'Y');

Currently you have it so Mouse extends Computer.


Your current Mouse constructor will only accept one value. If you want Mouse to accept both $type and $hasmouse, then you need to accept two values.

class Mouse extends Computer {
    protected $hasmouse = 'null';
    public function __construct($type, $hasmouse) {
        parent::__construct($type);
        $this->hasmouse = $hasmouse;
    }
    public function computermouse() {
         if($this->hasmouse == 'Y') {
             return 'This Computer has a mouse';
         }
    }
}

$pc = new Mouse('PC', 'Y');

I would also suggest using boolean (true/false) instead of 'Y'

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号