Association between classes with PHP

Asked

Viewed 700 times

1

I searched for content on class relationships, more specifically association, but all the examples I found were not properly encapsulated (attributes and methods declared public), I tried to make an association for myself in the way I believe to be correct, however when the 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;
    }
}

Instances of classes:

<?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 Supplier could not be converted to string in C: xampp htdocs Projeto index.php on line 26

I wonder where I’m going wrong.

  • What are the returned errors? Apparently one is missing ; at the end of echo of product information.

  • Catchable fatal error: Object of class Supplier could not be converted to string in C: xampp htdocs Projeto index.php on line 26

  • Edit the question and add this information. Also specify which line is 26.

  • "Supplier: {$Product->$Supplier->getRazao()}"; this is line 26, I can’t edit questions yet

  • The line you posted here is different from the one in the code. Why do you have this $ in front of Fornecedor? I guess you didn’t even post in the question the line that’s going wrong.

  • Only one attempt again unsuccessful.

  • Okay, but the code you posted is not the full code of index.php, is it? The error is on line 26 and you posted only 15 lines.

  • I only got what it says, regarding php, the rest is html created automatically by Netbeans

  • Okay. You set the attribute Fornecedor how private and are trying to access it directly? Have you tried using the method getFornecedor? Something like $Produto->getFornecedor()->getRazao()? Detail: in the method setFornecedor, the attribute Fornecedor is written in lower case.

  • now I got, thank you very much my friend, that’s exactly what I should do

Show 5 more comments

1 answer

0

On the line pointed by error

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

Fix it that way:

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

Upshot:

===========================
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 as it is different from the error that appeared in the environment where I ran. 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 it was obvious the error and I only used getter (getFornecedor()). And the question came, "Why doesn’t he use the getter if he stated it?". But then he gets into another subject..

The answer should end here, but I will quote other obvious mistakes as a precaution.

Other points

In this passage

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 emit error warnings that can stop execution depending on the environment setting.

Example:

public function blabla() {

}

Browser other questions tagged

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