New PHP Challenge by Innobyte

A few days ago, we launched a new PHP challenge.

We asked both our colleagues and Facebook community to redefine sad function in order to solve this problem and make the code work:

PHPChallenge

They found some cool and clever solutions in order to execute beAwesome() function:

Solution nr.1:

function sad() {

die(“Are you happy now ?”);

}

 

Solution 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()

}

 

Solution nr.3

function sad()

{

return false;

}

 

Solution 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!';

}

 

Solution 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();

}

 

Solution 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();

}

 

Do you have a better solution to solve this problem? Share it with us! 🙂

 

 

Scroll to Top