php-internals

print_r() adds properties to DateTime objects [duplicate]

荒凉一梦 提交于 2019-12-02 04:25:02
问题 This question already has answers here : Why can't I access DateTime->date in PHP's DateTime class? Is it a bug? (5 answers) Closed 2 years ago . Consider the following code sample: $m_oDate = new DateTime('2013-06-12 15:54:25'); print_r($m_oDate); echo $m_oDate->date; Since PHP 5.3, this produces (something like) the following output: DateTime Object ( [date] => 2013-06-12 15:54:25 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) 2013-06-12 15:54:25 However the following code: $m_oDate

print_r() adds properties to DateTime objects [duplicate]

我的梦境 提交于 2019-12-02 01:58:42
This question already has an answer here: Why can't I access DateTime->date in PHP's DateTime class? Is it a bug? 5 answers Consider the following code sample: $m_oDate = new DateTime('2013-06-12 15:54:25'); print_r($m_oDate); echo $m_oDate->date; Since PHP 5.3, this produces (something like) the following output: DateTime Object ( [date] => 2013-06-12 15:54:25 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) 2013-06-12 15:54:25 However the following code: $m_oDate = new DateTime('2013-06-12 15:54:25'); echo $m_oDate->date; ...simply emits an error: Notice: Undefined property: DateTime::

PHP Performance : Copy vs. Reference

时光怂恿深爱的人放手 提交于 2019-12-01 17:03:32
Hey there. Today I wrote a small benchmark script to compare performance of copying variables vs. creating references to them. I was expecting, that creating references to large arrays for example would be significantly slower than copying the whole array. Here is my benchmark code: <?php $array = array(); for($i=0; $i<100000; $i++) { $array[] = mt_rand(); } function recursiveCopy($array, $count) { if($count === 1000) return; $foo = $array; recursiveCopy($array, $count+1); } function recursiveReference($array, $count) { if($count === 1000) return; $foo = &$array; recursiveReference($array,

PHP Performance : Copy vs. Reference

走远了吗. 提交于 2019-12-01 15:49:00
问题 Hey there. Today I wrote a small benchmark script to compare performance of copying variables vs. creating references to them. I was expecting, that creating references to large arrays for example would be significantly slower than copying the whole array. Here is my benchmark code: <?php $array = array(); for($i=0; $i<100000; $i++) { $array[] = mt_rand(); } function recursiveCopy($array, $count) { if($count === 1000) return; $foo = $array; recursiveCopy($array, $count+1); } function

PHP Generators - Garbage Collection

梦想与她 提交于 2019-12-01 15:04:43
问题 Simple question. When or how, by PHP or yourself do generators destroy their stacks? Take the following example: function doWork(): Generator { // create some objects. $o1 = new stdClass(); $o2 = new stdClass(); // pause here and wait for data. $value = yield 1; // By referencing the above objects, they shouldn't destruct. $o1->property = $value; $o2->property = $value; yield $o1; yield $o2; // End of stack. } // Create the generator. $generator = doWork(); $value = $generator->current(); //

Is there ever a need to use ampersand in front of an object?

爱⌒轻易说出口 提交于 2019-12-01 04:41:57
Since objects are passed by reference by default now, is there maybe some special case when &$obj would make sense? Objects use a different reference mechanism. &$object is more a reference of a reference. You can't really compare them both. See Objects and references : A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned

Linking a PHP Extension Written in C

随声附和 提交于 2019-11-30 15:57:14
Edit: Revising My Question When building an external PHP module in C, how do I link shared objects? If your C extension code uses a shared library, you need to declare that in the config.m4 file. I strongly recommend using the ext_skel script that's included in the PHP source to generate a skeleton config.m4: ./ext_skel --extname=myextension Since you're linking to a library, by convention you should use the --with-myextension options (as opposed to --enable-myextension ). Uncomment the relevant lines in the config.m4 and fill in the details of your lib. Something like the following: # --with

Compare PHP Arrays Using Memory References

倖福魔咒の 提交于 2019-11-30 14:35:46
Is it possible to see if two array variables point to the same memory location? (they are the same array) Actually, this can be done. Through a php extension. File: config.m4 PHP_ARG_ENABLE(test, whether to enable test Extension support, [ --enable-test Enable test ext support]) if test "$PHP_TEST" = "yes"; then AC_DEFINE(HAVE_TEST, 1, [Enable TEST Extension]) PHP_NEW_EXTENSION(test, test.c, $ext_shared) fi File: php_test.h #ifndef PHP_TEST_H #define PHP_TEST_H 1 #define PHP_TEST_EXT_VERSION "1.0" #define PHP_TEST_EXT_EXTNAME "test" PHP_FUNCTION(getaddress4); PHP_FUNCTION(getaddress); extern

In which order are objects destructed in PHP?

一世执手 提交于 2019-11-30 12:33:44
问题 What is the exact order of object deconstruction? From testing, I have an idea: FIFO for the current scope. class test1 { public function __destruct() { echo "test1\n"; } } class test2 { public function __destruct() { echo "test2\n"; } } $a = new test1(); $b = new test2(); Which produces the same results time and time again: test1 test2 The PHP manual is vague (emphasis mine to highlight uncertainty): "The destructor method will be called as soon as there are no other references to a

Confusion about PHP 7 refcount

為{幸葍}努か 提交于 2019-11-30 07:18:50
<?php $s = "foobar"; $t = $s; $u = $s; echo PHP_VERSION . "\n"; debug_zval_dump($s); xdebug_debug_zval('s'); Run in PHP 5.6.16 Run in PHP 7.0.2 I think the result (PHP 7) should be: string(6) "foobar" refcount(4) s: (refcount=3, is_ref=0)="foobar" I wonder what makes the difference? Need some explanation. Thanks a lot. ------ update ------ Nikita Popov's - PHP 7 – What changed internally? (P41) http://www.slideshare.net/nikita_ppv/php-7-what-changed-internally In PHP 7 a zval can be reference counted or not. There is a flag in the zval structure which determined this. There are some types