3v4l.org

run code in 300+ PHP versions simultaneously
<?php class SomeClass { public $array; public $content; public $item; public function someMethod() { $this->array = [ "testing" => "testing one two three", "apples" => "red", "oranges" => "orange", ]; foreach($this->array as $this->item) { // I pass $this->item down inside the loop echo $this->content; // this here is looped } } } $class = new SomeClass(); /** * I want to access $this->item without having to do a loop * * I don't want this e.g: foreach($this->array as $this->item) { * echo $this->item; * } * * but instead I just want to do this: foreach($this->array as $this->item) { * echo $this->content; this here will output the content * } * * and inside `$class->content` it has the variable that is passed down: `$this->item`. * that way I don't HAVE to write a loop and all I have to do is write `$this->content`. * that means, I don't want to write a loop! I just want to write what the loop outputs! * and I'm having troubles getting the item that is passed inside of the loop. */ // I want to do this but I can't figure out how to access $class->item outside of loop $class->content = $class->item; // I could do something that works: foreach($class->array as $class->item) { echo $class->item; } // but I want to access `$class->item` outside of the loop and use it in: $class->content = $class->item; // then call the method that will loop and echo the property `content` $class->someMethod();
Output for git.master, git.master_jit, rfc.property-hooks
Warning: foreach() argument must be of type array|object, null given in /in/WTD0v on line 52

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
52.01 ms | 1730 KiB | 4 Q