Error in a Codeigniter library

1

I have the following library in codeigniter:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Breadcrumb {

private $breadcrumbs = array();
private $separator = '  >  ';
private $start = '<div id="breadcrumb">';
private $end = '</div>';

public function __construct($params = array()){
    if (count($params) > 0){
        $this->initialize($params);
    }
}

private function initialize($params = array()){
    if (count($params) > 0){
        foreach ($params as $key => $val){
            if (isset($this->{'_' . $key})){
                $this->{'_' . $key} = $val;
            }
        }
    }
}

function add($title, $href){  
    if (!$title OR !$href) return;
    $this->breadcrumbs[] = array('title' => $title, 'href' => $href);
}

function output(){
    if ($this->breadcrumbs) {
        $output = $this->start;
        foreach ($this->breadcrumbs as $key => $crumb) {
            if ($key){
                $output .= $this->separator;
            }

            if (end(array_keys($this->breadcrumbs)) == $key) {
                $output .= '<span>' . $crumb['title'] . '</span>';   
            }
            else {
                $output .= '<a href="' . $crumb['href'] . '">' . $crumb['title'] . '</a>';
            }
        }

        return $output . $this->end . PHP_EOL;
    }
    return '';
}
}

In the controller I insert the breadcrumbs:

public function index(){
    $this->breadcrumb->add('Home', base_url());
    $this->breadcrumb->add('Tutorials', base_url().'tutorials');  
    $this->breadcrumb->add('Spring Tutorial', base_url().'tutorials/spring-tutorials');
    $this->dados['breadcrumb']  =   $this->breadcrumb->output();
    $this->dados['conteudo']    =   'layouts/moderador/painel_controle_view';
    $this->load->view('layouts/layout_master',$this->dados);
}

And in the view I make the display:

<div id="conteudo" class="container principal">
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <p>PAINEL DE CONTROLE</p>
                <p><?= (!empty($breadcrumb)?$breadcrumb:''); ?></p>
            </div>
        </div>
    </div>
</div>

When I run the following message appears:

  

A PHP Error was encountered

     

Severity: Runtime Notice

     

Message: Only variables should be passed by reference

     

Filename: libraries / Breadcrumb.php

     

Line Number: 37

     

Backtrace:

     

File: D: \ wamp \ www \ PortalCompras \ application \ libraries \ Breadcrumb.php   Line: 37 Function: _error_handler

     

File:   D: \ wamp \ www \ PortalCompras \ application \ controllers \ moderator \ ControlPaint.php   Line: 13 Function: output

     

File: D: \ wamp \ www \ PortalCompras \ index.php Line: 315 Function:   require_once

I could not identify the error. Can anyone help me?

    
asked by anonymous 16.10.2017 / 14:32

1 answer

1

The problem is on the line:

if (end(array_keys($this->breadcrumbs)) == $key) {

The function end() according to the documentation only accepts references as argument, ie only variables return method or function are not valid. See the function signature:

  

mixed end (array & array)

That & means that the argument must be a variable and not a value, it happens with some frequency.

To solve this, simply assign the result of array_keys() to a new variable and then pass it to end() :

$keys = array_keys($this->breadcrumbs);
if(end($keys) == $key){

Related:

Doubt about PHP function 'end'

    
16.10.2017 / 14:38