Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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') and exit() 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();
    }
}
like image 808
Wallter Avatar asked Feb 07 '14 21:02

Wallter


1 Answers

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

like image 74
Mark Baker Avatar answered Oct 20 '22 23:10

Mark Baker