How to construct a class correctly with access methods?

2

How do I join multiple variables into a single object? How do I get the following code?

<?php
class Produto {
    //Atributos
    var $cpu;
    var $mb;
    var $psu;

    //Getters & Setters
    function setProduto($produto, $produto1){
        $this->cpu = $produto;
        $this->mb = $produto1;
    }


    function getProduto(){
        return $this->cpu;
        return $this->mb;
    } 
}?>

Code to access the class

<?php
include('exibindo_classes.php')
$produto = new Produto();
$produto->setProduto('Core i7 7700', 'Z270');

echo $produto->getProduto();
?>
    
asked by anonymous 14.09.2017 / 01:00

4 answers

10

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 ).

    
14.09.2017 / 01:12
6

I do not see much sense in getting behind-the-scenes getters and setters in PHP in many cases. Of course it will depend a lot on the context, on how the class was designed and on the intended use of its instances. Sometimes what you need is a simple object with public properties. For example:

<?php
class Produto {
    public $cpu;
    public $mb;
    public $psu;

    public function __construct($cpu, $mb, $psu) {
        $this->cpu = $cpu;
        $this->mb = $mb;
        $this->psu = $psu;
    }
}

$produto = new Produto('Core i7 7700', 'Z270', '500W');
echo $produto->cpu; // Core i7 7700
$produto->mb = 'Z271';
echo $produto->mb; // Z271

link

Sometimes even this, just an associative array:

$produto = [
    'cpu' => 'Core i7 7700',
    'mb' => 'Z270',
    'psu' => '500W'
];

echo $produto['cpu'];
$produto['mb'] = '...';
// ...
    
14.09.2017 / 03:32
1

I do not know if this is what you want, but the function can not have multiple returns, so you can use arrays. more or less like this:

function getProduto(){
    return array('variavelcpu' => $this->cpu, 'variavelmb' => $this->mb);
} 

Then you can access them like this:

$produtoretorno = $produto->getProduto();
echo $produtoretorno['variavelcpu'];
echo $produtoretorno['variavelmb'];

I do not know what to do.

    
14.09.2017 / 01:10
-1

Breaking the head a little .. I came to the following conclusion .. and that worked out by signal .. only that I'm not with separate files (the class in a separate file from the display) ..

<?php
class Produto {
    //Atributos
    private $cpu;
    private $mb;
    private $psu;

    //Getters & Setters
    function setProduto($produto, $produto1, $produto2){
        $this->cpu = $produto;
        $this->mb = $produto1;
        $this->psu = $produto2;
    }


    function getProduto(){
        return $this->cpu;
        return $this->mb;
        return $this->psu;
    } 

    function mostrarProduto(){
        echo $this->cpu .$this->mb .$this->psu;
    }
}

//Exibindo a classe
$produto = new Produto();
$produto->setProduto('Core i7 7700 <br />', 'Z270E STRIX <br />', 'CX750M <br />');
$produto->mostrarProduto();
?>

Am I correct in using this way ?? I'm doing a web development course .. I'm in the Object Orientation module in PHP .. Thanks for all the help you are giving me.

    
14.09.2017 / 01:32