/** * This test function contains a code which will * create a php warning to happen. This eample show * that in phpunit it will treated as an exception and * it could be catched by 'expectedException' tag and * exception name would be 'PHPUnit_Framework_Error' * * @expectedException PHPUnit_Framework_Error */ public function testPhpError() { $foo_obj = new Foo(); # Line below will throw an exception here only $foo_obj->bar(); # Code execution will never reach this point $this->assertTrue(true); }
function test_dynamic_new() { $f = new Foo(); $f->bar(); $k = "Foo"; $g = new $k(); $g->bar(); }
function testChainedPropertyAssignmentExpressionHasExpectedEndLine() { Foo::bar(__FUNCTION__)->baz(__FILE__)->value = 'FOOBAR'; }
<?php require 'EventCallableObject.php'; EventCallableObject::pushDirectory('.'); $foo = new Foo(); echo $foo->bar(1, 2, 3);
public function testRuleNotAppliesToDynamicMethodCall() { $foo = new Foo(); $foo->bar(); }
<?php class Foo { public static function bar() { echo "Hello"; } } // Instance level $f = new Foo(); $f::bar(); // Class level Foo::bar();
<?php class Foo { public function __call($method, $args) { if (isset($this->{$method})) { $func = $this->{$method}; $func($args); } } } $foo = new Foo(); $foo->bar = function () { echo "Hello, this function is added at runtime"; }; $foo->bar();
<?php class Foo { public static function bar($x) { if ($x == 1) { echo "hi"; } } } Foo::bar(1); Foo::bar(2);
<?php class Foo { private $test; public function __construct($test) { $this->test = $test; } public function bar() { return strrev($this->test); } } $foo = new Foo('Hello world'); return $foo->bar();
<?php namespace HHVM\UserDocumentation\Inconsistencies\Intro\Examples\LVP; class Foo { function bar($baz) { $baz = 'herpderp'; // Always outputs array('herpderp') var_dump(func_get_args()); } } $f = new Foo(); $f->bar('blah');
<?php class Foo { public $x = 0; function bar() { return function () { return $this->x; }; } } $foo = new Foo(); $qux = $foo->bar(); $foobar = new Foo(); $foobar->x = 3; var_dump($qux()); var_dump($qux->call($foo)); // Try on an object other than the one already bound var_dump($qux->call($foobar)); // Pass a non-object as the parameter for binding the closure to. // Will get a warning var_dump($qux->call(4)); var_dump($qux->call(null)); $bar = function () { return $this->x; }; $elePHPant = new StdClass(); $elePHPant->x = 7; // Try on a StdClass var_dump($bar->call($elePHPant));
<?php class Foo { public static function bar($x) { if ($x == 1) { echo "hi"; } else { if ($x == 2) { echo "hello"; } else { echo "uhhh"; } } } } Foo::bar(1); Foo::bar(2); Foo::bar(3);
function testArgumentsContainsPropertyPostfixExpression() { Foo::bar(Bar::$baz); }
<?php class Alliteration { public $firstAllit = "cast vicariously as both victim and villain"; public $clause = array('part1' => 'no mere veneer of vanity'); public $nested = array(array("is a vestige of the", "vox populi", "now vacant")); } class Foo { public function bar($bag_of_stuff) { echo 'some embedded newlines'; echo "\n"; echo 'Arnold once said: "I\'ll be back"'; echo "\n"; $v = "Voila! "; $all = new Alliteration(); echo "{$v} In view humble vaudevillian veteran" . "{$all->firstAllit} {$bag_of_stuff['0']}.\n"; echo "This visage, {$all->clause['part1']}, " . "{$all->nested[0][0]} \"{$all->nested[0][1]}\"" . " {$all->nested[0][2]}\n"; } } $f = new Foo(); $f->bar(array("by the vicissitudes of fate"));
<?php class Foo { var $bar = "bar"; function bar($what) { echo "I'm a ", $what, "!\n"; } } $foo = new Foo(); echo $foo->bar, "\n"; $foo->bar("function"); $v8 = new V8Js(); $v8->foo = new Foo(); $v8->executeString('print(PHP.foo.$bar, "\\n");'); $v8->executeString('PHP.foo.__call("bar", ["function"])'); // // Hello, v8 // class Foo { // var $bar = null; // } // // $v8 = new V8Js(); // $v8->foo = new Foo; // $v8->executeString('print( "bar" in PHP.foo ? "yes" : "no" );');
<?php class Foo { public function bar($n) { return $n + 1; } } class Hooking { public function __call($name, $args) { echo "Before {$name}!\n"; $r = call_user_func_array([$this, "_hook_{$name}"], $args); echo "After {$name}!\n"; return $r; } public static function hook($name) { runkit_method_rename(get_called_class(), $name, "_hook_{$name}"); } } runkit_class_adopt('Foo', 'Hooking'); Foo::hook('bar'); $foo = new Foo(); echo $foo->bar(10); echo "\n";
<?php class Foo { public $bar = 'propiedad'; public function bar() { return 'método'; } } $obj = new Foo(); echo $obj->bar, PHP_EOL, $obj->bar(), PHP_EOL;
<?php class Foo { public static function __callStatic($name, $args) { return $name . ' called statically'; } } echo Foo::bar(); // bar called statically
<?php require_once $GLOBALS["HACKLIB_ROOT"]; class Foo { public function bar() { (yield 5); return; } public static function baz() { if (false) { (yield false); } return; } } foreach (Foo::baz() as $v) { echo $v . "\n"; } echo "break\n"; $f = new Foo(); foreach ($f->bar() as $v) { echo $v . "\n"; }
public function testDummy() { $dependency = $this->prophesize(\Foo\DependencyInterface::class); $foo = new Foo($dependency->reveal()); $this->assertTrue($foo->bar()); }
function foo() { Foo::bar(); Foo::bar(); }
<?php /** * A simple test class */ class Foo { function bar() { } } $f = new Foo(); $b = $f->bar();
<?php class Foo { public function bar() { } } // new Foo()->bar() $f = new Foo(); $f->bar();
function testSuppressionControl() { // Turning off suppression before execution $chain = new ErrorControlChainTest_Chain(); $chain->setSuppression(false); list($out, $code) = $chain->then(function ($chain) { Foo::bar(); // Non-existant class causes fatal error })->executeInSubprocess(true); $this->assertContains('Fatal error', $out); $this->assertContains('Foo', $out); // Turning off suppression during execution $chain = new ErrorControlChainTest_Chain(); list($out, $code) = $chain->then(function ($chain) { $chain->setSuppression(false); Foo::bar(); // Non-existent class causes fatal error })->executeInSubprocess(true); $this->assertContains('Fatal error', $out); $this->assertContains('Foo', $out); }
<?php class Foo { var $bar = array(); static function bar() { static $instance = null; $instance = new Foo(); return $instance->bar; } } extract(Foo::bar()); echo "ok\n";
<? class Foo { protected $name = 'foo'; public function bar($m) { var_dump($this->name); $f = function() { var_dump($this); }; $f(); $m(); } } $foo = new Foo; $foo->bar(function() { var_dump($this); });
<?php trait Too { function bar() { $abc = 123; $a = function ($x) use($abc) { $n = func_num_args(); $args = func_get_args(); var_dump($n, $args); }; return $a; } function baz($obj) { $abc = 456; $obj(789); } } class Foo { use Too; } $a = Foo::bar(); Foo::baz($a);
break; case "common_1": case "common_2": echo "in common case"; break; case "fallthrough": echo "will fall through"; // FALLTHROUGH // FALLTHROUGH case "dummy": echo "dummy case"; break; default: echo "I guess this is the end, my friend"; } echo "\n"; $y = true; $z = 41; switch (true) { case foo($str): echo "helloooo"; break; case $y && $z + 1 == 42: echo "nevermind"; break; } echo "\n"; } } Foo::bar("specific");
<?php /** * Another file used for testing */ require_once "newfolder/subdir/things.php"; $b = Foo::bar();
<?php include __DIR__ . "/../vendor/autoload.php"; use G\IDisposable; class Bar implements IDisposable { public function hello($name) { return "Hello {$name}"; } public function dispose() { echo "Disponse Bar"; } } class Foo { public function bar() { using(new Bar(), function (Bar $bar) { $bar->hello("Gonzalo"); }); } } $foo = new Foo(); echo $foo->bar();