property is not a php object

0

I'm doing an echo of an object that is an html string, first I declare it in an array and then I convert it into an object, I do a var_dump and if it comes out but when doing the echo it goes out that the property is not an object. Can you tell me what I'm doing wrong? thank you very much.

htmlclientes.php

 class HtmlClientes extends Clientes
 {

    protected $item;

    public function htmlItem($numer)
    {
      var_dump($numer);
      if ($numer % 6 == 0) {
        return $this->item = (object) array(
            'itemtop' => '<li class="flex flex-center-horizontal logo8">',
            'itembottom' => '</li>'
        );
      }
    }
 }

index.php

 <?php
  $i = 0;
  foreach ($clientes->getDatos() as $cliente){
    $cliente = (object) $cliente;
    $html = $clientes->htmlItem($i);
    var_dump($html);

    echo $html->itemtop;
 ?>
 <img src="<?php echo $rootUrl; ?>assets/images/img<?php echo $cliente->imagen; ?>" alt="<?php echo $cliente->nombre; ?>" />
 <?php
    echo $html->itembottom;
    $i++;
  }
?>

result var_dump

int(0)
 object(stdClass)#5 (2) {
   ["itemtop"]=>
     string(46) "<li class="flex flex-center-horizontal logo8">"
   ["itembottom"]=>
     string(5) "</li>"
 }
    
asked by Albert Arias 16.02.2017 в 17:42
source

1 answer

2

I think the htmlItem function should return some value when the condition if ($ numer% 6 == 0) is not met or validate if the variable $ html it is not null, to then print the result.

Example:

Function:

public function htmlItem($numer)
{
  var_dump($numer);
  if ($numer % 6 == 0) {
    return $this->item = (object) array(
        'itemtop' => '<li class="flex flex-center-horizontal logo8">',
        'itembottom' => '</li>'
    );
  }
  else return null;
}

Output:

 <?php
  $i = 0;
  foreach ($clientes->getDatos() as $cliente){
    $cliente = (object) $cliente;
    $html = $clientes->htmlItem($i);
    var_dump($html);

    if($html == null)
     continue;


    echo $html->itemtop;
 ?>
 <img src="<?php echo $rootUrl; ?>assets/images/img<?php echo $cliente->imagen; ?>" alt="<?php echo $cliente->nombre; ?>" />
 <?php
    echo $html->itembottom;
    $i++;
  }
?>
    
answered by 16.02.2017 в 17:57