Error creating object with inheritances in PHP

0

I am developing an application with inheritances of objects. When you want to create a new object, do not create it. When I want to get the id of an object, it returns me:

  

Fatal error: Call to a member function getId () on a non-object.

Code where I call getId() :

<?php    
$controller = new TextController();
$c = $controller->getAllText();
?>
<h1> blog X</h1>
<a href="afegircontingut.html">afegir contingut</a>
<?php
  while($c){?>
<h2><?=$c->getId();?></h2>
<p><?=$c->getContingut();?></p>
  

The code that generates the object is this:

public function getAllText(){
    $this->connect();

    $s = $this->_conn->prepare("SELECT * FROM content");
    $s->execute();
    $result = $s->get_result();

     $rows =  array();
     while($row = $result->fetch_assoc()) {
         array_push($rows,$this->createTextFromAssoc($row));
     }
     return $rows;
}

private function createTextFromAssoc($arr){
    $text = new Text($arr['id'], $arr['titol'], $arr['data'], $arr['autor'], $arr['tipus'], $arr['contingut']);
    return $text;
}

ContentInterface.php interface:

<?php

    interface ContentInterface {
    public function getId();
    public function setId($id);

    public function getTitol();
    public function setTitol($titol);

    public function getData();
    public function setData($data);

    public function getAutor();
    public function setAutor($autor);

    public function getTipus();
    public function setTipus($tipus);

    public function getContingut();
    public function setContingut($contingut);
    }

Abstract class ContentAbstract.php:

 class ContentAbstract implements ContentInterface{
 protected $id;
 protected $titol;
 protected $data;
 protected $autor;
 protected $tipus;
 protected $contingut;


    public function __construct($id = null, $titol = null, $data= null, $autor = null, $tipus = null, $contingut= null)
{
          $this->setId($id);
          $this->setTitol($titol);
          $this->setData($data);
          $this->setAutor($autor);
          $this->setTipus($tipus);
          $this->setContingut($contingut);

}

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

public function getTitol(){
    return $this->titol;

}
public function setTitol($titol){
    $this->id= $id;
}

public function getData(){
    return $this->data;

}
public function setData($data){
    $this->id= $id;

}

public function getAutor(){
    return $this->autor;        
}
public function setAutor($autor){
    $this->autor= $autor;
}

public abstract function getTipus();
public abstract function setTipus($tipus);

public abstract function getContingut();
public abstract function setContingut($contingut);
}

classe Text.php:

class Text extends ContentAbstract{

public function  __construct(){
   parent::__construct();
 }

public function getTipus(){
    return "text";
}

public function setTipus($tipus){
    $this->tipus= $tipus;
}

public function getContingut(){
    return "contingut text";
}

public function setContingut($contingut){
    $this->contingut= $contingut;
}
}
    
asked by francesc 31.12.2017 в 15:41
source

2 answers

0

In ContentAbstract.php you have several set that repeat the setId() .

public function setTitol($titol){ $this->id= $id; }

public function setData($data){ $this->id= $id; }

They should assign $titol and $data .

    
answered by 31.12.2017 в 15:51
0

The error is because line while($c){ is wrong.
The variable% co_of% is a% co_of% of objects% co_of%.
Since $c is a array , it does not have the Text method.

Solution:

To iterate the fix you could use $c to access the objects array within it.

Example:

<?php
foreach($c as $text){?>
<h2><?=$text->getId();?></h2>
<p><?=$text->getContingut();?></p>
    
answered by 31.12.2017 в 17:01