PHP Version 5.4.13 Bug? CodeIgniter Bug? Or, Explanation?

483

So... I pushed some code live the other day (that worked 100% fine on my local machine) but killed the server - no Codeigniter Logs, no Apache Logs,die('msg') andexit() did not work - I have never experienced this before in 5+ years of PHP development.

After 50+ commits to my repo I narrowed the problem down to one statement that works when split apart, but not together.

System Info:

PHP Version: 5.4.13

Codeigniter Version:define('CI_VERSION', '2.1.3');

These lines work (being called in a Codeigniter MY_Controller function):

dump($this->get_val('order_id')); 
$tmp = $this->get_val('order_id');
dump($tmp); 
dump(empty($tmp)); 
dump(!empty($tmp));

But when I add this following line the above described crash happens:

!empty($this->get_val('order_id'))

This seems like a PHP bug?


Structure:

Main.php

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Main extends Administration {

    function index() {
        // if (!empty($in['order_id'])) { /*  BROKEN  */
        dump($this->get_val('order_id'));
        $tmp = $this->get_val('order_id');
        dump($tmp);
        dump(empty($tmp));
        dump(!empty($tmp));
        // dump(!empty($this->get_val('order_id'))); /*  BROKEN  */
        // if (!empty($this->get_val('order_id'))) { /*  BROKEN  */
        //     dump(true);
        // } else {
        //     dump(false);
        // }
    }
}

adminstration.php

<?php

class Administration {

    /**
     *
     * @var MY_Controller;
     */
    public $ci;
    public $p;

    function __construct() {
        $this->ci = & get_instance();

        $this->ci->load->model('user/admin/user_admin_model');

        $this->p = $this->ci->uri->uri_to_assoc(4);
    }

    protected function get_val($name = '') {
        $pst = (array) $this->ci->input->post();
        $gt = (array) $this->ci->input->get();

        if (empty($name)) {
            return array_merge($pst, $gt, $this->p);
        }

        if (!empty($this->p[$name]))
            return $this->p[$name];
        if (!empty($pst[$name]))
            return $pst[$name];
        if (!empty($gt[$name]))
            return $gt[$name];

        return array();
    }

}

?>

My_Controller.php

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class MY_Controller extends CI_Controller {

    protected $p;

    function __construct() {
        parent::__construct();
        $this->p = $this->uri->uri_to_assoc();
    }

    function get_val($name = '') {
        dump("I am get_val in MY_controller");

        $pst = (array) $this->input->post();
        $gt = (array) $this->input->get();

        if (empty($name)) {
            return array_merge($pst, $gt, $this->p);
        }

        if (!empty($this->p[$name]))
            return $this->p[$name];
        if (!empty($pst[$name]))
            return $pst[$name];
        if (!empty($gt[$name]))
            return $gt[$name];

        return array();
    }
}
750

Answer

Solution:

Prior to PHP version 5.5.0, empty only worked on variables, not on the returned value from a function or directly on the result of an expression

People are also looking for solutions to the problem: php - Encapsulation issues

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.