Edit report at https://bugs.php.net/bug.php?id=63394&edit=1

 ID:                 63394
 Updated by:         ras...@php.net
 Reported by:        razpusni dot bg at gmail dot com
 Summary:            Trait crash
-Status:             Closed
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Ubuntu
 PHP Version:        5.4.8
 Block user comment: N
 Private report:     N



Previous Comments:
------------------------------------------------------------------------
[2012-10-30 10:21:34] razpusni dot bg at gmail dot com

XCache causes the problem. Removing it from php configuration solved the issue.

------------------------------------------------------------------------
[2012-10-30 10:08:27] 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 09:37:33] 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 ...


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63394&edit=1

Reply via email to