php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63394 Trait crash
Submitted: 2012-10-30 09:37 UTC Modified: 2012-10-30 12:52 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: razpusni dot bg at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.8 OS: Ubuntu
Private report: No CVE-ID: None
 [2012-10-30 09:37 UTC] razpusni dot bg at gmail dot com
Description:
------------
Bug 1: 

When require_once('traits/tDB.php'); is called:

Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.

Solution 1:
-> removing
   protected function initialize(){
        echo 'initializing';
   }
Solution 2:
-> inserting trait code directly into main file not in separate one also works. 

Bug 2:
file Singleton.php - trait
file Database.php - class

When calling Database::getInstance() in browser:
1) First execution of the script works fine - non cached probably
2) Second file execution causes:
Fatal error: Call to undefined method Database::getInstance() in ...

When renaming trait makes the script work but again only on first execution.

Test script:
---------------
Bug 1:
trait tDB {
    
    protected $db;
    
    function __construct($db = null) {
        echo 'here';
        if($db === null)
            $this->db = Database::getInstance();
        else
            $this->db = $db;
            
        $this->initialize();
    }
    
    protected function initialize(){
        echo 'initializing';
    }

}

Bug 2:
File Singleton.php
<?php

namespace traits;

trait Singleton {
    
    protected static $instance;
    
    final public static function getInstance($init = false, $args = null) {
        if($init !== false){
            if(!is_array($args))
                static::$instance->init();
            else
                static::$instance->init($args);
        }
        
        return isset(static::$instance)
               ? static::$instance
               : static::$instance = new static;
    }
                                                
    protected static function init() {}
    
}

?>

<?php
class Database {
    use \traits\Singleton;
    //.... some public methods
}
?>

Expected result:
----------------
Bug 1:
ability to declare initialize() in separate file.
Bug 2:
Database::getInstance() being callable.

Actual result:
--------------
Bug 1:
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
Bug 2:
Fatal error: Call to undefined method Database::getInstance() in ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-30 10:08 UTC] razpusni dot bg at gmail dot com
Version:
PHP 5.4.8-1~precise+1 (cli) (built: Oct 29 2012 15:34:15)
 [2012-10-30 10:21 UTC] razpusni dot bg at gmail dot com
XCache causes the problem. Removing it from php configuration solved the issue.
 [2012-10-30 10:21 UTC] razpusni dot bg at gmail dot com
-Status: Open +Status: Closed
 [2012-10-30 12:52 UTC] rasmus@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC