Association between classes with PHP

0

I searched for content about relationship between classes, more specifically association, but all the examples I found were not properly encapsulated (attributes and methods declared as public), I tried to do for myself an association of the form I believe to be correct, however when objects are instantiated PHP returns errors:

The association between two classes Produto and Fornecedor :

Class Produto :

class Produto {
    private $nome, $valor, $Fornecedor, $id;

    function __construct($nome, $valor, $Fornecedor, $id) {
        $this->nome = $nome;
        $this->valor = $valor;
        $this->Fornecedor = $Fornecedor;//Associação
        $this->id = $id;
    }

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

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

    function setValor($valor)
    {
        $this->valor = $valor;
    }

    function getValor()
    {
        return $this->valor;
    }

    function setFornecedor($fornecedor){
        $this->fornecedor = $fornecedor;
    }

    function getFornecedor()
    {
        return $this->Fornecedor;
    }

    function setId($id)
    {
        $this->id = $id;
    }

    function getId()
    {
        return $this->id;
    }
}

Class Fornecedor :

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Fornecedor
 *
 * @author AbelSouzaCostaJúnior
 */
class Fornecedor {
    private $rs, $id, $endereco, $telefone;//Rs → razão social

    function __construct($rs, $id, $endereco, $telefone) {
        $this->rs = $rs;
        $this->id = $id;
        $this->endereco = $endereco;
        $this->telefone = $telefone;
    }

    function setRazao($rs)
    {
        $this->rs = $rs;
    }

    function getRazao()
    {
        return $this->rs;
    }

    function setId($id)
    {
        $this->id = $id;
    }

    function getId()
    {
        return $this->id;
    }

    function setEndereco($endereco)
    {
        $this->endereco = $endereco;
    }

    function getEndereco()
    {
        return $this->endereco;
    }

    function setTelefone($telefone)
    {
        $this->telefone = $telefone;
    }

    function getTelefone()
    {
        return $this->telefone;
    }
}

Class Instances:

<?php
    require_once 'Produto.php';
    require_once 'Fornecedor.php';

    $Fornecedor = new Fornecedor("Mercado da Casa", 359, "Rua B", 32210273);
    $Produto = new Produto("Café 250g", 1.99, $Fornecedor, 03331);

    echo "===========================<br>"
    . "Informaçõs sobre o produto<br>"
            . "Código do produto: {$Produto->getId()}"
            . "<br>"
            . "Produto: {$Produto->getNome()}"
            . "<br>"
                    . "Fornecedor: {$Produto->Fornecedor->getRazao()}"   // Linha 26
?>
  

Catchable fatal error: Object of class Vendor could not be converted to string in C: \ xampp \ htdocs \ Project \ index.php on line 26

I'd like to know where I'm going wrong.

    
asked by anonymous 21.05.2017 / 20:08

1 answer

0

In the line marked by error

. "Fornecedor: {$Produto->Fornecedor->getRazao()}"

Please correct this:

. "Fornecedor: {$Produto->getFornecedor()->getRazao()}";

Result:

===========================
Informaçõs sobre o produto
Código do produto: 1753
Produto: Café 250g
Fornecedor: Mercado da Casa

This should solve the main problem. I just find the error message strange because it is different from the error that appeared in the environment where I executed it. PHP7.1.0 builtin server, Android (app: Server for PHP).

The error here was for trying to access a property defined as private. From there the error was obvious and I only used the getter (% with%). And the question came, "Why does not he use the getter if he declared it?" But that's another matter.

The answer should end here, but I will cite other obvious errors as a precaution.

Other points

In this section

function setFornecedor($fornecedor){
    $this->fornecedor = $fornecedor;
}

The property starts with a capital letter. It should look like this:

function setFornecedor($fornecedor){
    $this->Fornecedor = $fornecedor;
}

Also fix the visibility of properties and methods because without a definition, current versions of PHP issue error warnings that may interrupt execution depending on the configuration of the environment.

Example:

public function blabla() {

}
    
21.05.2017 / 21:26