|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-11-03 00:07 UTC] flobee at gmail dot com
Description:
------------
calling methodes on destruction is difficult to understand when using singelton patterns which does not make sence for me. maybe i'm wrong.. i'm not sure at the moment...
also: if session is used this fails on destruction
see script below for a simple test
i expect the call of knock() from master class on destruction of class b.
when not using singelton patter all is fine. the order of the destruction works.
this problems starts with php 5.5* packages in debian testing.
$ uname -a
Linux d1 3.10-3-686-pae #1 SMP Debian 3.10.11-1 (2013-09-10) i686 GNU/Linux
$ php -v
PHP 5.5.5-1 (cli) (built: Oct 19 2013 22:09:48)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
Test script:
---------------
<?php
error_reporting( -1);
session_start();
interface Singleton_Interface
{
public static function getInstance();
}
abstract class Singleton implements Singleton_Interface
{
public function __clone()
{
throw new Exception( 'Clone is not allowed.' );
}
}
class master extends Singleton implements Singleton_Interface
{
private $r;
private static $instance;
public static function getInstance( $params=array( ) )
{
if( !isset(self::$instance) ) {
$c = __CLASS__;
self::$instance = new $c($params);
}
return self::$instance;
}
public function knock()
{
echo 'call ' . __METHOD__ . PHP_EOL;
}
public function __construct($r=false)
{
$this->r = $r;
echo __METHOD__ . PHP_EOL;
}
public function __destruct()
{
echo __METHOD__ . PHP_EOL;
if (method_exists($this->r, 'knock')) {
$this->r->knock();
}
$_SESSION['x'] = __METHOD__;
}
}
class b extends Singleton implements Singleton_Interface
{
private $r;
private static $instance;
public static function getInstance( $params=array( ) )
{
if( !isset(self::$instance) ) {
$c = __CLASS__;
self::$instance = new $c($params);
}
return self::$instance;
}
public function knock()
{
echo 'call ' . __METHOD__ . PHP_EOL;
}
public function __construct($r=false)
{
$this->r = $r;
echo __METHOD__ . PHP_EOL;
}
public function __destruct()
{
echo __METHOD__ . PHP_EOL;
if (method_exists($this->r, 'knock')) {
$this->r->knock();
}
$_SESSION['y'] = __METHOD__;
}
}
header( 'content-type: text/plain' );
echo 'session:'; print_r($_SESSION);
$master = master::getInstance();
$b = b::getInstance($master); // calls master->knock() on destruction
$master->knock();
$b->knock();
echo 'done' . PHP_EOL;
Actual result:
--------------
/* result:
master::__construct
b::__construct
call master::knock
call b::knock
done
master::__destruct
b::__destruct
call master::knock <------ no error?
_session is empty! correct? wrong?
*/
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 18:00:01 2025 UTC |
note, i just found some hints in my code and which make me think the following: public function __destruct() { // does not work; looses mysql-link first well, so: custom code stays longer (in a kind of way to be used) than php and the needed functionality. this is the same with the session. Am I wrong or missing some docs for that kind of reasons?