This is very wrong. My suggestion is to focus on the fundamentals, to do the procedural well, when to see that you need to and to realize that you already have mastered the basics so think about OOP that is less useful than is spoken, still less in PHP, and more difficult than looks. Making OOP right is so hard to do that it usually comes out better not to use. Of course there are problems that greatly benefit from OOP and in general can be very useful when the person already has enough experience in everything else.
For example, you still lack conditions to give good names to things, which is something basic and should be mastered before doing OOP.
Almost always getters and setters is not as good an idea in PHP as it is a Java. There are even properties in PHP that would be a little better. I'll leave it the way you want it, but I'll advance that this is the way that almost everyone does, but it is not the most correct way. And almost everyone does this because they do not learn correctly, they only reproduce what others do wrong too.
It is very difficult to make OOP right without seeing the whole, without knowing every detail, without doing everything as one thing, which does not have this code, but to take a step:
<?php
class Produto {
private $cpu;
private $mb;
private $psu;
public function __construct($cpu, $mb) {
$this->cpu = $cpu;
$this->mb = $mb;
}
function getCpu() {
return $this->cpu;
}
function setCpu($cpu) {
$this->cpu = cpu;
}
function getMb() {
return $this->mb;
}
function setMb($mb) {
$this->mb = mb;
}
}
$produto = new Produto('Core i7 7700', 'Z270');
echo $produto->getCpu();
?>
See running on ideone . And no Coding Ground . Also put it on GitHub for future reference .
Note that I've used a builder . This is how an object starts. You do not just have getters and a setter for the entire object, you have one for each attribute. If they are needed. Eventually a __toString()
could be useful, among other things, but people often use it wrong ( see , and more here ).