|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-10-12 21:44 UTC] aparcos at gmail dot com
Description:
------------
A segmentation fault is going to happen when you accidentally uses clone inside the jsonSerialize method that implements JsonSerializable and return the new cloned object. If you uncomment the following test script, you will see that is going to be called several times until the recursive call get exhausted and finish given the segmentation fault.
Test script:
---------------
<?php
class CloneThis implements JsonSerializable {
public $date;
public function jsonSerialize() {
$clone = clone $this;
// var_dump($this);
// if ($this->date instanceof DateTime) {
// $clone->date = $this->date->format('Y.m.d');
// }
return $clone;
}
}
$variable = new CloneThis;
$variable->date = new DateTime();
echo json_encode($variable);
Expected result:
----------------
I think should be :
1. or the current date.
2. or An error where many recursive calls are done (maybe)
Actual result:
--------------
////////////////////////////////
...strings removed
object(CloneThis)#52273 (1) {
["date"]=>
string(10) "2019.10.12"
}
object(CloneThis)#52274 (1) {
["date"]=>
string(10) "2019.10.12"
}
object(CloneThis)#52275 (1) {
["date"]=>
string(10) "2019.10.12"
}
object(CloneThis)#52276 (1) {
["date"]=>
string(10) "2019.10.12"
}
object(CloneThis)#52277 (1) {
["date"]=>
Segment violation
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 23:00:01 2025 UTC |
Ok! this is a programmer error but php should throw PHP Fatal Error, at least some known like: PHP Fatal error: Maximum function nesting level of '256' reached, aborting! in - on line 3 PHP Stack trace: PHP 1. {main}() -:0 PHP 2. foo() -:6 but not a Segmentation Fault, doesn't?