<?php
$stack = new SplStack();
$stack->add(0, 'one');
$stack->add(1, 'two');
$stack->add(2, '2015');
echo "<pre>";
print_r($stack);
When i run this code, all items are stored an property called dllist
(i hope) and it's the expected result.
当我运行此代码时,所有项目都存储了一个名为dllist的属性(我希望),这是预期的结果。
SplStack Object
(
[flags:SplDoublyLinkedList:private] => 6
[dllist:SplDoublyLinkedList:private] => Array
(
[0] => one
[1] => two
[2] => 2015
)
)
But i run the code below, works the same way that code above, why when i try, $stack[] = 'one';
why does not get an error? how php know that $stack[] = 'one';
should be stored in dllist
?
但我运行下面的代码,工作方式与上面的代码相同,为什么当我尝试时,$ stack [] ='one';为什么不出错? php如何知道$ stack [] ='one';应该存储在dllist中?
<?php
$stack = new SplStack();
$stack[] = 'one';
$stack[] = 2015;
$stack[] = 'two';
echo "<pre>";
print_r($stack);
I expected some errors like this statements:
我期待像这样的声明一些错误:
<?php
$int = 2015;
$int[] = 300; //Warning: Cannot use a scalar value as an array in
$str = 'foo';
$str[] = 'foo'; //Fatal error: [] operator not supported for strings in
$obj = new stdClass();
$obj[] = 'new one'; //Fatal error: Cannot use object of type stdClass as array in
1 个解决方案
#1
1
SplStack implements the interface ArrayAccess, causing the SplStack class to have array-features: http://php.net/manual/en/class.arrayaccess.php
SplStack实现接口ArrayAccess,导致SplStack类具有数组功能:http://php.net/manual/en/class.arrayaccess.php
#1
1
SplStack implements the interface ArrayAccess, causing the SplStack class to have array-features: http://php.net/manual/en/class.arrayaccess.php
SplStack实现接口ArrayAccess,导致SplStack类具有数组功能:http://php.net/manual/en/class.arrayaccess.php