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 ofecho
of product information.– Woss
Catchable fatal error: Object of class Supplier could not be converted to string in C: xampp htdocs Projeto index.php on line 26
– Abel SCJ
Edit the question and add this information. Also specify which line is 26.
– Woss
"Supplier: {$Product->$Supplier->getRazao()}"; this is line 26, I can’t edit questions yet
– Abel SCJ
The line you posted here is different from the one in the code. Why do you have this
$
in front ofFornecedor
? I guess you didn’t even post in the question the line that’s going wrong.– Woss
Only one attempt again unsuccessful.
– Abel SCJ
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.– Woss
I only got what it says, regarding php, the rest is html created automatically by Netbeans
– Abel SCJ
Okay. You set the attribute
Fornecedor
how private and are trying to access it directly? Have you tried using the methodgetFornecedor
? Something like$Produto->getFornecedor()->getRazao()
? Detail: in the methodsetFornecedor
, the attributeFornecedor
is written in lower case.– Woss
now I got, thank you very much my friend, that’s exactly what I should do
– Abel SCJ