How to build a class correctly with access methods?

Asked

Viewed 151 times

2

How do I join several variables into a single object? How do I arrange 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();
?>

4 answers

10


This is so wrong. My suggestion is to focus on the fundamentals, on doing the procedural very well, when you see that you need it and realize that you already know the basics so think about OOP which is less useful than speaking, even less in PHP, and more difficult than it seems. Doing OOP right is so hard to do that it usually comes out better not to use. Of course there are problems that benefit a lot from OOP and in general can be very useful when one already has enough experience in everything else.

For example, it still lacks 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 in Java. There are even properties in PHP that would be a little better. I’ll leave it the way you want, but I already say that this is the way that almost everyone does, but it is not the most correct form. And almost everyone does it because they don’t learn properly, they’re just reproducing what others do wrong too.

It is very difficult to do OOP right without seeing the whole, without knowing every detail, without doing everything as one thing, which does not have in 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();
?>

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that I used a builder. This is how you start an object. You don’t just have one getters and a Setter for the whole object, you have one for each attribute. If necessary. Possibly a __toString() could be useful, among other things, but almost always people use wrong (see, and more here).

  • If the intention is to use the object more or less as a struct, I would remove all getters and setters and directly use the properties - which are public in that example, because declared with var.

  • That’s what I’d do. I’ll fix it.

  • got it.. that’s what I was wondering. if you have a get and a set for each attribute.

  • @Stormwind I don’t know if I understand what you want to know.

  • 2

    @Stormwind then depends on context, correct without context is always wrong :) As I said, in PHP I would not even have a class even, if I had a reason for a class, I think the constructor would be like this, but has this example is not good to base. The right depends on many concrete factors and this is too abstract. That’s why I say everyone learns OOP wrong, everyone teaches wrong, teaches with abstract examples, then one thinks it is so. Even knowing how to do it right is hard to do it right, I miss all the time, my only advantage is that I recognize it :D

6

I don’t see much point in creating getters and setters in PHP in many cases. Of course it will depend a lot on the context, how the class was designed and what use you intend to make 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

https://ideone.com/aD5cGk

Sometimes not even that, just an associative array:

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

echo $produto['cpu'];
$produto['mb'] = '...';
// ...
  • 1

    I went to the focus of the question, but I probably wouldn’t even create the class, in PHP, so that’s the answer I like the most ;)

  • I thought that counterpoint was missing here :)

  • This last example, from the associative array, basically does the same thing as a class with Magic getters/setters, if there is no more logic in these accessors. Which is practically a expandObject.

1

I don’t know if it’s what you want, but the function can’t 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'];

From what I understand you’re needing would be this, if it’s something else, try to give more details.

  • Vlw Fernando.. more I’m just practicing what I learned in a class.. then n understood how q could be done the way I did or if it was just by array msm..

  • I understand @Jpfreitas, is that when you put the return in the function, anything below it will not be executed. For example if you put a Return and below it a line with echo, that bass line will never be read.

-1

cracking my head a little.. I came to the following conclusion.. and that worked out by the way.. only that I n am with separate files(the class in a separate file from the view)..

<?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 am taking a WEB development course.. I am in the Object Orientation module in PHP.. I appreciate all the help you’re giving me.

  • your getProduct() is once again wrong, you are using several Return, which ends up returning only the first line 'Core i7 7700 <br />' and ignoring the other two. But it is not being used anywhere in your code. O <br /> that you used in the setProduct, should be treated within the class, and not when adding the product. , more or less like this: $Product->setProduct('Core i7 7700', 'Z270E STRIX', 'CX750M'); And then inside showProduct(), you can put it like this: echo $this->cpu . '<br />'. $this->Mb . '<br />'. $this->psu;

  • I get it... vlw..

  • @Jpfreitas is not, it seems that took part of what I did, but kept some things wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.