How to relate category instance to a product instance in PHP

Asked

Viewed 294 times

3

I have 2 classes in PHP, Product and Category.

Following each other’s code.

Product.php

class Produto {
     public $nome;
     public $categoria;
}

Category.php

class Categoria {
     public $nome;
}

In that case, what I just want to do is refer to the category that will be instantiated later in the product category attribute. How can I do that?

Just to clarify something, a product needs only have one category, it doesn’t need to have more than one.

  • 2

    You want your object Produto receive the object Categoria? If yes, you’re right now, just include the class path Categoria and treat it normally as an object within Produto.

  • You in the title say you want to add 2 categories in a product, however did not clarify this in the description, it would be just that?

3 answers

5


You could do as follows using encapsulation, where to be set all class properties as private or protected(If there is an inheritance relation) and be create the methods that have become responsible for manipulating these properties, thus ensuring the integrity of the data, which in this case are the set’s and get’s methods, where:

Set’s: These are the methods responsible for assigning values in class properties, where this allows you to process values before they are assigned to your properties, thus ensuring greater security of the integrity of your class’s data.

Get’s: They are the methods responsible for allowing reading properties outside the class, thus enabling you to create only get’s for the properties you want to be read outside the class.

Product class:

<?php

class Produto {

  private $nome;
  private $categoria;

  //Método construtor da classe Produto
  public function __construct($nome, Categoria $categoria) {
    $this->nome = $nome;
    $this->categoria = $categoria;
  }

  public function setNome($nome) {
    $this->nome = $nome;
  }

  public function getNome() {
    return $this->nome;
  }

  public function setCategoria(Categoria $categoria) {
    $this->categoria = $categoria;
  }

  public function getCategoria() {
    return $this->categoria;
  }

}

Class Category:

<?php

class Categoria {

  private $nome;

  //Método construtor da classe Categoria
  public function __construct($nome) {
    $this->nome = $nome;
  }

  public function setNome($nome) {
    $this->nome = $nome;
  }

  public function getNome() {
    return $this->nome;
  }

}

Test file:

<?php

require_once 'Produto.php';
require_once 'Categoria.php';

$categoria = new Categoria('Livro');
$produto = new Produto('Sistema de Banco de Dados', $categoria);

echo 'Produto: ' . $produto->getNome();
echo '<br>';
echo 'Categoria: ' . $produto->getCategoria()->getNome();
  • 1

    the best proposal so far, since it uses getters and setters, it would be good to add an explanation of why, +1

  • This proposal is very clear. Here I should not also use a destructor method too?

  • 2

    The destructor is not required. Use only if you need to finish some connection, or take something out of memory, or generate logs, etc

  • php allows typing now? Interesting, not even knew, to too outdated hehe

  • Yes, but only with type induction using class.

3

I advise you to use builders, if I understand well the category property in the Product is another class "Category", then you can do so:

class Categoria {
 public $nome;

 public __construct($nome) {
  $this->nome = $nome;
 }
}

Ai when you create a product you can put like this:

$produto = new Produto();
$produto->categoria = new Categoria("Nome");

I hope I understand correctly.

3

For the product to have several categories if understood right, just do the following:

class Produto{
    public $nome;
    public $categoria = array();

    public adicionarCategoria(Categoria $Categoria)
    {
        $this->categoria[] = $Categoria;
    }

    public function __toString()
    {
        return $this->nome;
    }
}

I recommend adding the __toString() method to make it easier when working user output.

class Categoria{
    public $nome;

    public function __toString()
    {
        return $this->nome;
    }
}

To add a category to a product would look like this:

$Categoria1 = new Categoria();
$Categoria1->nome = 'Categoria 1';

$Categoria2 = new Categoria();
$Categoria2->nome = 'Categoria 2';

$Produto = new Produto();
$Produto->adicionarCategoria($Categoria1);
$Produto->adicionarCategoria($Categoria2);
  • Your explanation is very good, but it wasn’t necessarily adding more than one category what I asked. But it can be very useful if I need to implement it. Your answer will help a lot in another situation.

  • 1

    All right, I was in doubt because you mentioned 2 categories in the title.

Browser other questions tagged

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