pthreads - PHP Bug in threaded environment assigning value to parent static member in same thread? -



pthreads - PHP Bug in threaded environment assigning value to parent static member in same thread? -

i have issue wonder if php bug or intentional behaviour. if assign in kid class mysql resource parent fellow member value gets lost when running thread.

this code not run - mysql resource should displayed twice:

<?php class myfileabstract extends threaded { protected static $m_connection; public static function init() { static::openfile(); echo "<br>member accessed parent: "; print_r(self::$m_connection); # resource empty } } class myfile extends myfileabstract { public static function openfile() { self::$m_connection = fopen("/etc/php.ini", "r"); echo "<br>member accessed child: "; print_r(self::$m_connection)."<br>"; # resource has value } } class mythread extends thread { public function run() { myfile::init(); } } $mythread = new mythread(); $mythread->start(); echo "<br>correct output:"; myfile::init(); ?>

this result - expected output "member accessed parent:" should "resource id#2":

member accessed child: resource id #2 fellow member accessed parent: right output: fellow member accessed child: resource id #3 fellow member accessed parent: resource id #3

i have alter , fellow member $m_connection receives/keeps resource fopen(). note: working in thread!

<?php class myfileabstract extends threaded { protected static $m_connection; public static function openfile() { $sfilename = static::getfilename(); self::$m_connection = fopen($sfilename, "r"); echo "member accessed parent: "; print_r(self::$m_connection); # resource has value } } class myfile extends myfileabstract { public static function getfilename() { homecoming "/etc/php.ini"; } } class mythread extends thread { public function run() { myfile::openfile(); } } $mythread = new mythread(); $mythread->start(); ?>

output:

member accessed parent: resource id #2

requirements run examples:

php compiled enabled thread safety php compiled pthread library

i have connect mysql in thread according article: http://php.net/manual/en/intro.pthreads.php

update:

i changed code: opening /etc/php.ini instead of connecting mysql database. same result --> resource gets lost when returning extended class. examples running 1:1 without changing/adapting anything.

i still think bug. found workaround, when resource explicitly assigned class fellow member in openfile() function parent:: , not self:::

<?php class myfileabstract extends threaded { protected static $m_connection; public static function init() { static::openfile(); echo "<br>member accessed parent: "; print_r(self::$m_connection); # resource empty } } class myfile extends myfileabstract { public static function openfile() { # resource keeps value when using parent:: instead of self:: parent::$m_connection = fopen("/etc/php.ini", "r"); echo "<br>member accessed child: "; print_r(parent::$m_connection)."<br>"; } } class mythread extends thread { public function run() { myfile::init(); } } $mythread = new mythread(); $mythread->start(); echo "<br>correct output:"; myfile::init(); ?>

the result correct:

member accessed child: resource id #2 fellow member accessed parent: resource id #2 right output: fellow member accessed child: resource id #3 fellow member accessed parent: resource id #3

php pthreads extending-classes

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -