Ieri am lansat o noua provocare PHP.
I-am rugat pe colegii nostri, dar si pe prietenii de pe pagina de facebook Innobyte sa redefineasca functia sad astfel incat sa rezolve problema de mai jos, pentru a face codul sa functioneze.
Am primit mai multe solutii creative pentru a executa functia beAwesome(). Iata cateva dintre ele:
Solutia nr.1:
1 2 3 4 5 |
function sad() { die(“Are you happy now ?”); } |
Solutia nr.2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Class s { Public static c=1; Public function stop() {} } Function sad (){ if (s::c ==1) { s::c = 2; return true; } Else return new s() } |
Solutia nr.3:
1 2 3 4 5 6 7 |
function sad() { return false; } |
Solutia nr.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php Class TestClass { public static $c = 0; public static function switchC() { self::$c = abs(self::$c - 1); } public function stop() { echo 'Stop! '; return false; } } function sad() { TestClass::switchC(); if (TestClass::$c == 1) { return true; } return new TestClass(); } function beAwesome() { echo 'Be awsome! :)'; } if (sad() === true) { sad()->stop(); beAwesome(); } else { echo 'Ooops!'; } |
Solutia nr.5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php class human { public function stop() { print("http://youtu.be/DgvePeDIt3Y"); } } function sad() { $return = isset($GLOBALS["sad_called"]) && $GLOBALS["sad_called"] === true ? new human() : true; $GLOBALS["sad_called"] = true; return $return; } function beAwesome() { print(" Who's your daddy?\n"); } if (sad()===true) { sad()->stop(); beAwesome(); } |
Solutia nr.6:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<?php /** * Class Sad * * */ class Sad { /** * @var boolean */ protected $isSad = true; /** * Stop the sadness */ public function stop() { $this->isSad = false; } /** * Check if sad * * @return boolean */ public function isSad() { return $this->isSad; } } /** * Weird function that returns true when an object was created... * * @return boolean|Sad */ function sad() { static $sad; if ($sad instanceof Sad) { return $sad; } $sad = new Sad(); return true; } /** * Method inspired by http://make-everything-ok.com/ */ function beAwesome() { echo PHP_EOL . PHP_EOL . (sad()->isSad() ? 'awesomeness could not be started... :(' : 'awesomeness started...' ). PHP_EOL; } // actual (given) code if (sad() === true) { sad()->stop(); beAwesome(); } |
Ai o solutie mai buna pentru aceasta problema? Impartaseste-o cu noi!
Salutare, cam tarziu, stiu, dar acum am dat peste acest articol.
Method chaining este secretul pentru a rezolva exact problema, cu o mica aditie…
Nu se poate scapa de $this in PHP ca sa arate exact ca in poza codul (vezi constructor), nu stiu daca exista un limbaj in care sa faci inlantuire cu operatorul -> fara sa ai $this (sau this, dar operatorul -> se va adauga in ES6 asa ca nu este posibil momentan) pentru definirea contextului:
sad() === true) {
$this->sad()->stop();
$this->beAwesome();
}
}
public function sad()
{
$this->caller = “sad”;
if($this->state == true) {
return $this;
} else {
$this->state = true;
return true;
}
}
public function stop()
{
echo “Stopped being ” . $this->caller . “!\n”;
}
public function beAwesome() {
echo “I’m awesome!”;
}
}
$a = new Life();
?>
Copy paste fara deschiderea php, se pare ca functia sanitara a crezut ca vreu sa fac injection 😀
class Life
{
private $caller = “”;
private $state = false;
public function __construct()
{
///// life motto
if ($this->sad() === true) {
$this->sad()->stop();
$this->beAwesome();
}
}
public function sad()
{
$this->caller = “sad”;
if($this->state == true) {
return $this;
} else {
$this->state = true;
return true;
}
}
public function stop()
{
echo “Stopped being ” . $this->caller . “!\n”;
}
public function beAwesome() {
echo “I’m awesome!”;
}
}
$a = new Life();
Buna, Adrian! Iti multumim, interesanta solutie! 🙂