php-internals

How does PHP memory actually work

社会主义新天地 提交于 2019-11-27 13:38:28
问题 I've always heard and searched for new php 'good writing practice', for example: It's better (for performance) to check if array key exists than search in array, but also it seems better for memory too: Assuming we have: $array = array ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, ); this allocates 1040 bytes of memory, and $array = array ( 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', ); requires 1136 bytes I understand that the key and value surely will have different storing

Why does a PHP array get modified when it's element is reference-assigned?

自闭症网瘾萝莉.ら 提交于 2019-11-27 08:19:23
问题 When ref-assigning an array's element, the contents of the array are modified: $arr = array(100, 200); var_dump($arr); /* shows: array(2) { [0]=> int(100) // ← ← ← int(100) [1]=> int(200) } */ $r = &$arr[0]; var_dump($arr); /* shows: array(2) { [0]=> &int(100) // ← ← ← &int(100) [1]=> int(200) } */ Live run. (Zend Engine will do fine, while HHVM shows "Process exited with code 153".) Why is the element modified? Why do we see &int(100) instead of int(100) ? This seems totally bizarre. What's

How are associative arrays implemented in PHP?

不想你离开。 提交于 2019-11-27 04:14:18
Can someone explain how PHP implements associative arrays? What underlying data structure does PHP use? Does PHP hash the key and store it in some kind of hash map? I am curious because I was wondering what the performance of associative arrays where when inserting and searching for keys. It's a hash table. The type declaration and hashing function are here: http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_hash.h?view=markup There is a light weight array and a linked list within the spl (standard php lib) Well, for what it is worth, all PHP arrays are Associative arrays. The highest voted

Getting Started with PHP Extension-Development [closed]

我是研究僧i 提交于 2019-11-27 03:42:08
Please suggest help articles or tutorials about PHP "low" level С-modules programming interface. Pascal MARTIN Searching through my bookmarks, only links I found are those : Extension Writing Part I: Introduction to PHP and Zend Extension Writing Part II: Parameters, Arrays, and ZVALs Extension Writing Part II: Parameters, Arrays, and ZVALs [continued] Extension Writing Part III: Resources Wrapping C++ Classes in a PHP Extension If you are really interested by the subject, and ready to spend some money on it, you could buy the book Extending and Embedding PHP ( some pages are available as

Where can I learn about PHP internals? [closed]

旧巷老猫 提交于 2019-11-26 23:53:16
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . What are good resources to start learning the internals of PHP and the Zend Engine? 回答1: The PHP Manual has a (sadly mostly empty)

What is #<some-number> next to object(someClass) in var_dump of an object? I have an inference. Am I right?

♀尐吖头ヾ 提交于 2019-11-26 20:47:13
This is the code & its output I used to draw the inference below: class a { public $var1; public $var2; } $obj0 = new a; var_dump($obj0); class b { public $var1; public $var2; public $var3; } $obj1 = new b; var_dump($obj1); $obj2 = new stdClass; var_dump($obj2); $obj3 = new stdClass; var_dump($obj3); $obj4 = new stdClass; var_dump($obj4); $obj5 = new stdClass; var_dump($obj5); var_dump(new stdClass); $obj6 = new stdClass; var_dump($obj6); The output: object(a)#1 (2) { ["var1"]=> NULL ["var2"]=> NULL } object(b)#2 (3) { ["var1"]=> NULL ["var2"]=> NULL ["var3"]=> NULL } object(stdClass)#3 (0) {

Why don't PHP attributes allow functions?

我的梦境 提交于 2019-11-26 16:32:16
I'm pretty new to PHP, but I've been programming in similar languages for years. I was flummoxed by the following: class Foo { public $path = array( realpath(".") ); } It produced a syntax error: Parse error: syntax error, unexpected '(', expecting ')' in test.php on line 5 which is the realpath call. But this works fine: $path = array( realpath(".") ); After banging my head against this for a while, I was told you can't call functions in an attribute default; you have to do it in __construct . My question is: why?! Is this a "feature" or sloppy implementation? What's the rationale? The

print_r() adds properties to DateTime objects [duplicate]

被刻印的时光 ゝ 提交于 2019-11-26 15:31:02
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::

Parentheses altering semantics of function call result

对着背影说爱祢 提交于 2019-11-26 14:28:24
It was noted in another question that wrapping the result of a PHP function call in parentheses can somehow convert the result into a fully-fledged expression, such that the following works: <?php error_reporting(E_ALL | E_STRICT); function get_array() { return array(); } function foo() { // return reset(get_array()); // ^ error: "Only variables should be passed by reference" return reset((get_array())); // ^ OK } foo(); I'm trying to find anything in the documentation to explicitly and unambiguously explain what is happening here. Unlike in C++, I don't know enough about the PHP grammar and

Detecting whether a PHP variable is a reference / referenced

南笙酒味 提交于 2019-11-26 08:56:34
问题 Is there a way in PHP to determine whether a given variable is a reference to another variable and / or referenced by another variable? I appreciate that it might not be possible to separate detecting \"reference to\" and \"reference from\" given the comment on php.net that setting $a=& $b means \" $a and $b are completely equal here. $a is not pointing to $b or vice versa. $a and $b are pointing to the same place. \" If it\'s not possible to determine whether a given variable is a reference