Error in a Codeigniter library

Asked

Viewed 132 times

1

I have the library next 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 Readcrumbs:

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 exhibition:

<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 run is appearing the following message:

PHP Error was encountered

Severity: Runtime Notice

Message: Only variables should be passed by Ference

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 moderador Painel_controle.php Line: 13 Function: output

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

I couldn’t identify the mistake. Can someone help me?

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 an argument, that is, only variables of the return of method or function are not valid. View 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 it is quite simple to assign the result of array_keys() into a new variable and then pass it to end():

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

Related:

Doubt about PHP function 'end'

  • Your tip fixed my problem. I did exactly as you indicated and the error message disappeared.

Browser other questions tagged

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