[php-src] Issue #10743: Order of Execution
| From: | donfromvestal | Date: | Wed, 01 Mar 2023 17:07:10 +0000 |
| Subject: | [php-src] Issue #10743: Order of Execution | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-243830@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/10743
Author: donfromvestal
### Description
The following code:
```php
<?php
class tab {
public $tab;
public function __construct (){
$this->tab='';
}
public function addTab($n) {
$this->tab .= '\t';
echo "addTab($n) :$this->tab:\n";
}
public function subTab($n) {
// \t is 2 characters
$this->tab = substr($this->tab,2);
echo "subTab($n) :$this->tab:\n";
}
public function clearTab() {
$this->tab='';
}
public function __toString() {
return $this->tab;
}
}
$tab = new tab();
class A {
public $a;
public $b;
public $c;
public function __construct(){
$this->a = 'a';
$this->b = 'b';
$this->c = 'c';
}
public function __toString(){
global $tab;
$output = array();
echo "A $tab\n";
$output[] = $tab."<$this->a>";
$tab->addTAb('A1');
$output[] = $tab."<$this->b>";
$tab->subTAb('A2');
$output[] = $tab."<$this->c>";
return implode("\n",$output);
}
}
class D {
public $d;
public $e;
public $f;
public $A;
public function __construct(){
$this->d = 'd';
$this->e = 'e';
$this->f = 'f';
$this->A = new A();
}
public function __toString(){
global $tab;
$output = array();
$output[] = $tab."<$this->d>";
$tab->addTAb('D1');
$output[] = $tab."<$this->e>";
$tab->addTAb('D2');
echo "D $tab\n";
$output[] = $this->A; // these two lines execute out of order?
$tab->addTAb('D3'); // these two lines execute out of order?
$output[] = $tab."<$this->f>";
return implode("\n",$output);
}
}
$D = new D();
echo "D:\n$D\n";
?>
/*
Hardware:
Mac Mini platform
OS:
macOS Montery
Version 12.6.3 (21G419)
PHP:
I found it on php 8.1.6 running on a MacBookPro High Sierra
I installed the latest php 8.2.3 and it still behaves the same
I have not tried it on php 8.0.*
php -v
[play8.txt](https://github.com/php/php-src/files/10863558/play8.txt)
PHP 8.2.3 (cli) (built: Feb 15 2023 00:30:25) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.3, Copyright (c) Zend Technologies
with Zend OPcache v8.2.3, Copyright (c), by Zend Technologies
order of operations Demo:
php play8.php
addTab(D1) :\t:
addTab(D2) :\t\t:
D \t\t // All good so far
addTab(D3) :\t\t\t: // addTab(D3) before - output[]=$this-A
A \t\t\t // $this->A (__toString())
addTab(A1) :\t\t\t\t:
subTab(A2) :\t\t\t:
D:
<d>
\t<e> // good to here
\t\t\t<a> // note the extra tab from addTab(D3) before A->__toString()
\t\t\t\t<b>
\t\t\t<c>
\t\t\t<f>
Expected output:
php play8.php
addTab(D1) :\t:
addTab(D2) :\t\t:
D \t\t
A \t\t
addTab(A1) :\t\t\t:
subTab(A2) :\t\t:
addTab(D3) :\t\t\t:
D:
<d>
\t<e>
\t\t<a>
\t\t\t<b>
\t\t<c>
\t\t\t<f>
*/
```
`Resulted in this output:
addTab(D1) :\t:
addTab(D2) :\t\t:
D \t\t // All good so far
addTab(D3) :\t\t\t: // addTab(D3) before - output[]=$this-A
A \t\t\t // $this->A (__toString())
addTab(A1) :\t\t\t\t:
subTab(A2) :\t\t\t:
D:
<d>
\t<e> // good to here
\t\t\t<a> // note the extra tab from addTab(D3) before A->__toString()
\t\t\t\t<b>
\t\t\t<c>
\t\t\t<f>
`
```
But I expected this output instead:
`addTab(D1) :\t:
addTab(D2) :\t\t:
D \t\t
A \t\t
addTab(A1) :\t\t\t:
subTab(A2) :\t\t:
addTab(D3) :\t\t\t:
D:
<d>
\t<e>
\t\t<a>
\t\t\t<b>
\t\t<c>
\t\t\t<f> ``
``
Hardware:
Mac Mini platform
OS:
macOS Montery
Version 12.6.3 (21G419)
PHP:
php -v
PHP 8.2.3 (cli) (built: Feb 15 2023 00:30:25) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.3, Copyright (c) Zend Technologies
with Zend OPcache v8.2.3, Copyright (c), by Zend Technologies
I found it on php 8.1.6 running on a MacBookPro High Sierra
I installed the latest php 8.2.3 and it still behaves the same
I have not tried it on php 8.0.*
### PHP Version
PHP 8.2.3
### Operating System
MacOS Montery