Skip to content

Instantly share code, notes, and snippets.

@oskarcalvo
Created October 3, 2017 10:40
Show Gist options
  • Save oskarcalvo/58573502f728a5c43eaf511ce9ae483a to your computer and use it in GitHub Desktop.
Save oskarcalvo/58573502f728a5c43eaf511ce9ae483a to your computer and use it in GitHub Desktop.
<?php
<?php
namespace SwitchClass;
$time_start = microtime(true);
echo memory_get_usage(), "\n";
/**
* Created by PhpStorm.
* User: oskar
* Date: 3/10/17
* Time: 10:28
*/
abstract Class SwitchClass {
private $data = NULL;
private $method;
function __construct(){}
private function DefaultMethod(){}
}
class alternancia extends SwitchClass {
private $data = NULL;
private $method;
function __construct($method, array $data)
{
$this->method = $method;
$this->data = $data;
if(method_exists($this, $this->method)){
$return = $this->{$method};
}else{
$return = $this->DefaultMethod();
}
return $return;
}
private function wadus() {
return $this->data;
}
private function wadus2() {
return $this->data;
}
private function wadus3() {
return $this->data;
}
private function DefaultMethod() {
return $this->data;
}
}
$method = 'wadus';
$array = ['wadus'];
$clase = new alternancia($method, $array);
var_dump($clase);
var_dump('esto es un anónimo');
$method = 'wadus444';
$clase = new alternancia($method, $array);
var_dump($clase);
$time_end = microtime(true);
$execution_time = ($time_end - $time_start)/60;
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';
echo "\n";
echo memory_get_usage(), "\n";
@carlescliment
Copy link


class Wadus1 {

  public function respondsTo($command) {
    return $command == 'wadus1';
  }

  public function execute() { ... }
}

class Wadus2 {

  public function respondsTo($command) {
    return $command == 'wadus2';
  }

  public function execute() { ... }
}

class Wadus3 {

  public function respondsTo($command) {
    return $command == 'wadus3';
  }

  public function execute() { ... }
}

class Default {

  public function respondsTo($command) {
    return true;
  }

  public function execute() { ... }
}


class Main {

  public function __construct() {
    $this->commands = [new Wadus1(), new Wadus2(), new Wadus3(), new Default()]
  }

  public function execute($instruction) {
    foreach ($command in $this->commands) {
      if ($command->respondsTo($instruction)) {
        return $command->execute($instruction);
      }
    }
}


(new Main())->execute('wadus1')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment