public function test() { $bar = new Bar(); $bar->setMessage('foobar'); $this->assertEquals('foobar', $bar->getMessage()); $this->assertEquals('foobar', $bar->message); }
public function doSomething() { $this->logger->info("Entering application."); $bar = new Bar(); $bar->doIt(); echo "<p>done</p>"; $this->logger->info("Exiting application."); }
function main() { $k = new Bar(); $y = new Baz(); foo(); $k->asd(); $y->k(); }
function read($id) { global $output; $output .= "read" . PHP_EOL; $a = new Bar(); $b = new Foo($a); $a->set($b); $session = array('old' => $b); return msgpack_serialize($session); }
protected function eraseData() { Foo::truncate(); Bar::truncate(); Baz::truncate(); Bom::truncate(); }
public static function getInstance() { if (isset(self::$instance)) { return self::$instance; } return self::$instance = new self(); }
public static function invoke(Foo $instance) { // Same class, other instance, direct call. $instance->call(); // Same class, other instance, indirect call. $foo = new Foo(); $foo->call(); // Same class, other instance, indirect call. $x = new Foo(); $y = $x; $z = $y; $z->call(); // Same class, other instance, direct call. (new Foo())->call(); // Other class, other instance, indirect call. $bar = new Bar(); $bar->call(); // Other class, other instance, direct call. (new Bar())->call(); }
public function test_decorator() { $foo = new Foo(); $bar = Bar::wrap($foo); // internal object $this->assertEquals($foo, $bar->object); // original property $this->assertEquals($bar->a1, 10); // new property $this->assertEquals($bar->b, 20); // redifine original property $this->assertEquals($bar->a2, 22); // original function $this->assertEquals($bar->fooFunction(), 14); // isset $this->assertTrue(isset($bar->b)); $this->assertTrue(isset($bar->a1)); }
/** * Bar constructor. * @param Session $session * @param string[] $barData */ public function __construct($session, $barData) { $this->Session = $session; $this->DB = $session->DB; if (empty(array_diff_key(Bar::ValidArray(), $barData))) { //Loading an existing bar $this->Refresh($barData); $this->Valid = true; } else { if (empty(array_diff_key(Bar::NewArray(), $barData))) { //Creating a new bar $this->Type = $barData['type']; $this->Title = $barData['title']; $this->Description = $barData['description']; $this->CreateStamp = $this->ModifyStamp = (double) microtime(true); $id = (int) $this->DB->Query("\n INSERT INTO tblBars\n (userID, type, title, description, createStamp, modifyStamp)\n VALUES(\n " . (int) $this->Session->ID . "\n , " . $this->DB->Quote($this->Type) . "\n , " . $this->DB->Quote($this->Title) . "\n , " . $this->DB->Quote($this->Description) . "\n , {$this->CreateStamp}\n , {$this->ModifyStamp}\n )\n ", true); if ($id) { $this->ID = $id; $this->Valid = true; } } } }
/** * Gets the 'foo_with_inline' service. * * This service is shared. * This method always returns the same instance of the service. * * @return \Foo A Foo instance */ protected function getFooWithInlineService() { $a = new \Bar(); $this->services['foo_with_inline'] = $instance = new \Foo(); $a->setBaz($this->get('baz')); $a->pub = 'pub'; $instance->setBar($a); return $instance; }
<?php require __DIR__ . "/classes.php.inc"; class Foo { public static function test() : A { return new A(); } } class Bar extends Foo { public static function test() : A { return new B(); } } var_dump(Bar::test()); var_dump(Foo::test());
$mail->Subject = 'Somebody sent you a token!'; $mail->Body = 'HEY YOU GUYS! LOOK WAT I DONE DID!!!!!!.'; $mail->AltBody = 'Received token.'; $email = $_POST['email']; $name = $_POST['name']; if (!$mail->send()) { $message = 'Message could not be sent. <p>'; } else { $message = 'Message has been sent.'; } return $app['twig']->render("email.html.twig", array('message' => $message)); }); $app->post("/add_preferred_bar/{id}", function ($id) use($app) { $user = Patron::find($id); $all_bars = Bar::getAll(); $bar = Bar::find($_POST['add_bar']); $user->addPreferredBar($bar); return $app['twig']->render("patron.html.twig", array('user' => $user, 'user_tokens' => $user->getTokens(), 'all_bars' => $all_bars, 'preferred_bars' => true, 'send_token' => false, 'token_form' => false, 'edit_user' => false)); }); // $app->delete("/delete_preferred_bar/{id}/{bar_id}", function($id, $bar_id) use($app) { // $user = Patron::find($id); // $all_bars = Bar::getAll(); // $bar = Bar::find($bar_id); // $user->deleteBar($bar); // return $app['twig']->render("patron.html.twig", array( // 'user' => $user, // 'user_tokens' =>$user->getTokens(), // 'all_bars' => $all_bars, // 'preferred_bars' => false, // 'send_token' => false, // 'token_form' => false,
{ return "FooBar2::ping();"; } function pang() { return "FooBar2::pang();"; } } class FooBar3 extends Bar { function cheer() { return "FooBar3::cheer();"; } } $b = new Bar(); $f = $b->create(); $fb = new FooBar(); $fb2 = new FooBar2(); $fb3 = new FooBar3(); check::equal($fb->used(), "Foo::pang();Bar::pong();Foo::pong();FooBar::ping();", "bad FooBar::used"); check::equal($fb2->used(), "FooBar2::pang();Bar::pong();Foo::pong();FooBar2::ping();", "bad FooBar2::used"); check::equal($b->pong(), "Bar::pong();Foo::pong();Bar::ping();", "bad Bar::pong"); check::equal($f->pong(), "Bar::pong();Foo::pong();Bar::ping();", "bad Foo::pong"); check::equal($fb->pong(), "Bar::pong();Foo::pong();FooBar::ping();", "bad FooBar::pong"); $method = new ReflectionMethod('Bar', 'ping'); check::equal($method->isProtected(), true, "Foo::ping should be protected"); $method = new ReflectionMethod('Foo', 'ping'); check::equal($method->isProtected(), true, "Foo::ping should be protected"); $method = new ReflectionMethod('FooBar', 'pang'); check::equal($method->isProtected(), true, "FooBar::pang should be protected");
function test_references() { // basic references (belongs_to) $f = new Foo(array('id' => 1, 'name' => 'Joe')); $f->put(); $b = new Bar(array('id' => 1, 'name' => 'Jim', 'foo' => 1)); $b->put(); $this->assertEquals($b->name, 'Jim'); $this->assertEquals($b->foo, 1); $this->assertEquals($b->foo()->name, 'Joe'); $this->assertEquals($b->foo()->name, 'Joe'); // fake reference should fail try { $this->assertTrue($b->fake()); } catch (Exception $e) { $this->assertRegExp('/Call to undefined method Bar::fake in .+tests\\/ModelTest\\.php on line [0-9]+/', $e->getMessage()); } }
<?php class Foo { protected function f() { return 'Foo'; } } trait T { public function f() { return 'Bar'; } } class Bar extends Foo { use T; } $b = new Bar(); echo $b->f() . "\n";
} }); //Sign Up Routes: $app->get("/show_customer_signup", function () use($app) { return $app['twig']->render('index.html.twig', array('about' => false, 'sign_up' => true, "sign_in" => false, 'team' => false, 'customer_signup' => true, 'business_signup' => false)); }); $app->get("/show_business_signup", function () use($app) { return $app['twig']->render('index.html.twig', array('about' => false, 'sign_up' => true, "sign_in" => false, 'team' => false, 'customer_signup' => false, 'business_signup' => true)); }); $app->post("/customer_signup", function () use($app) { $new_user = new Patron($_POST['username'], $_POST['email']); $new_user->save(); return $app['twig']->render("signup_confirmation.html.twig"); }); $app->post("/business_signup", function () use($app) { $new_bar = new Bar($_POST['name'], $_POST['phone'], $_POST['address'], $_POST['website']); $new_bar->save(); return $app['twig']->render("signup_confirmation.html.twig"); }); //Get Show email search $app->get("/show_email_search/{id}", function ($id) use($app) { $user = Patron::find($id); $all_bars = Bar::getAll(); return $app['twig']->render("patron.html.twig", array('user' => $user, 'user_tokens' => $user->getTokens(), 'all_bars' => $all_bars, 'preferred_bars' => false, 'send_token' => true, 'token_form' => false, 'edit_user' => false)); }); //Get Show User Tokens $app->get("/show_user_tokens/{id}", function ($id) use($app) { $user = Patron::find($id); $tokens = $user->getTokens(); return $app['twig']->render("patron.html.twig", array('user' => $user, 'user_tokens' => $tokens, 'preferred_bars' => false, 'send_token' => false, 'token_form' => true, 'edit_user' => false)); });
<?php require_once $GLOBALS['HACKLIB_ROOT']; function var_foo($x) { $arg_arr = func_get_args(); return $x + count($arg_arr); } class Bar { public static function var_bar() { return count(func_get_args()); } } function var_php_variadic($foo, ...$bar) { return count($bar); } echo var_foo(5) . "\n"; echo var_foo(5, "hi", array(4, 2), null, true) . "\n"; echo Bar::var_bar(1, 2, 3) . "\n"; echo Bar::var_bar() . "\n"; echo var_php_variadic("10") . "\n"; echo var_php_variadic("10", 1, array()) . "\n"; echo var_php_variadic("10", 1, array(), ...array(1, 2, 3)) . "\n";
<?php class Foo { public function printItem($string) { echo 'Foo: ' . $string . PHP_EOL; } public function printPHP() { echo 'PHP is great.' . PHP_EOL; } } class Bar extends Foo { public function printItem($string) { echo 'Bar: ' . $string . PHP_EOL; } } $foo = new Foo(); $bar = new Bar(); $foo->printItem('baz'); // Output: 'Foo: baz' $foo->printPHP(); // Output: 'PHP is great' $bar->printItem('baz'); // Output: 'Bar: baz' $bar->printPHP(); // Output: 'PHP is great'
} class Foo { public function __construct() { Stat::getInstance(); } } class Error { private $trace; public function __construct() { $this->trace = debug_backtrace(1); } } class Bar { public function __destruct() { Stat::getInstance(); new Error(); } public function test() { new Error(); } } $foo = new Foo(); $bar = new Bar(); $bar->test();
public function doSomething() { $bar = new Bar(); $bar->doSomething(); echo __METHOD__; }
<?php class Foo { public final function f() { return 'Foo'; } } class Bar extends Foo { public final function f() { return 'Bar'; } } $bar = new Bar(); echo $bar->f() . "\n";
use IterMethods; public static function hacklib_initialize_statics() { self::hacklib_initialize_statics_IterMethods(); } } Foo::hacklib_initialize_statics(); class Bar implements \HH\KeyedIterable, HH\Unrelated { use IterMethods; public static function hacklib_initialize_statics() { self::hacklib_initialize_statics_IterMethods(); } } Bar::hacklib_initialize_statics(); } namespace blah { class Baz implements \HH\KeyedIterable, \HH\Unrelated { use \IterMethods; public static function hacklib_initialize_statics() { self::hacklib_initialize_statics_IterMethods(); } } Baz::hacklib_initialize_statics(); testKeys(new \Foo()); testKeys(new \Bar()); testKeys(new Baz()); }
<?php class Foo { public function f() { return 'Foo'; } } trait T { protected function f() { return 'Bar'; } } class Bar extends Foo { use T; function inv() { return $this->f(); } } $b = new Bar(); echo $b->inv() . "\n";
<?php trait T { public final function foo() { return 'T::foo'; } } class Foo { use T; } class Bar extends Foo { public final function foo() { return 'Bar::foo'; } } $bar = new Bar(); echo $bar->foo() . "\n";
echo "ReflectionFunction(1):"; var_dump($rf->getStaticVariables()); foo(); echo "ReflectionFunction(2):"; var_dump($rf->getStaticVariables()); foo(); $rf = new ReflectionFunction('foo'); echo "ReflectionFunction(3):"; var_dump($rf->getStaticVariables()); echo "---- ReflectionMethod ----\n"; $rf = new ReflectionMethod('Bar', 'foo'); echo "ReflectionMethod(1):"; var_dump($rf->getStaticVariables()); Bar::foo(); echo "ReflectionMethod(2):"; var_dump($rf->getStaticVariables()); Bar::foo(); $rf = new ReflectionFunction('foo'); echo "ReflectionMethod(3):"; var_dump($rf->getStaticVariables()); echo "---- ReflectionClosure ----\n"; $rf = new ReflectionFunction($foo); echo "ReflectionFunction(1-closure):"; var_dump($rf->getStaticVariables()); $foo(); echo "ReflectionFunction(2-closure):"; var_dump($rf->getStaticVariables()); $foo(); $rf = new ReflectionFunction($foo); echo "ReflectionFunction(3-closure):"; var_dump($rf->getStaticVariables());
public function testUnset() { $foo = Foo::get('foo3'); $this->assertEquals(2, count($foo->bars)); $this->assertEquals('Bar 1', $foo->bars[0]->barbar); $this->assertEquals('Bar 2', $foo->bars[1]->barbar); unset($foo->bars[0]); $foo = Foo::get('foo3'); $bar = Bar::get('bar 1'); $this->assertEquals(1, count($foo->bars)); $this->assertEquals('Bar 2', $foo->bars[0]->barbar); $this->assertEquals(1, count($bar->foos)); $this->assertEquals('Foo 2', $bar->foos[0]->foofoo); }
<? // // enhanced method resolution using // $this and static hints echo Foo::blob(); // Foo echo Bar::blob(); // Bar echo $o->gaga(); // Foo echo $p->blob(); // not resolvable class Foo { function blob() { return $_GET['in_foo']; } function gaga() { return $this->blob(); } } class Bar { function blob() { return $_GET['in_bar']; } } ?>
function foo() { $bar = new Bar(); $bar->bar(); bar(); }
/** * Gets the 'inlined' service. * * This service is shared. * This method always returns the same instance of the service. * * This service is private. * If you want to be able to request this service from the container directly, * make it public, otherwise you might end up with broken code. * * @return \Bar A Bar instance. */ protected function getInlinedService() { $this->services['inlined'] = $instance = new \Bar(); $instance->setBaz($this->get('baz')); $instance->pub = 'pub'; return $instance; }
} /** * Enter description here ... */ function _private_foo() { } // When calling class constructors with no arguments, always include // parentheses. $bar = new Bar(); $bar = new Bar($arg1, $arg2); $bar = 'Bar'; $foo = new $bar(); $foo = new $bar($i, $i); // Static class variables use camelCase. Bar::$basePath = '/foo'; // Concatenation - there has to be a space. $i . "test"; $i . 'test'; $i . $i; $i . NULL; // It is allowed to have the closing "}" on the same line if the class is empty. class MyException extends Exception { } // Nice alignment is allowed for assignments. class MyExampleLog { const INFO = 0; const WARNING = 1; const ERROR = 2;