How to relate category instance to a product instance in PHP

3

I have 2 classes in PHP, Product and Category.

Following the code for each.

Product.php

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

Category.php

class Categoria {
     public $nome;
}

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

Just by clarifying one thing, a product only needs to have a category, it does not have to have more than one.

    
asked by anonymous 13.11.2015 / 13:11

3 answers

4

You could do as follows using encapsulation, where you set all the properties of the class to private or protected (if there is an inheritance relation) and create the methods that were 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 the values in the properties of the class, where this allows you to handle the values before they are assigned to their properties, thus ensuring greater security of integrity of your class data.

Get's : These methods are responsible for allowing properties to be read out of the class, thus enabling you to create only the get's for the properties you want to 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();
    
13.11.2015 / 13:48
3

I would advise you to use constructors, if I understood the category property well in the Product is another "Category" class, then you could do so:

class Categoria {
 public $nome;

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

Then when you create a product you can put it like this:

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

I hope I understand correctly.

    
13.11.2015 / 13:17
3

In order for the product to have several categories if I understand it 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 for the user to work out the 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);
    
13.11.2015 / 13:23