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:
function sad() {
die(“Are you happy now ?”);
}
Solutia nr.2:
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:
function sad()
{
return false;
}
Solutia nr.4:
<?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:
<?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:
<?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! 🙂