Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Shorthand Switch

I'm looking for more comfortable/more short version of Switch() statement in case of using multiple functions.

I'll give you one example: imagine 100-200 functions in one class, and you want to call only one of them by setting value to id in that class.

In my particular case, I have the following structure of PHP file:

<?php

  class _main
  {
      function request($id)
      {
          switch($id)
          {
              case 0:
                  $this->writeA();
              break;
              case 1:
                  $this->writeB();
              break;

              ///...
              // then we have 100-200 functions like this in switch.
          }
      }
      function writeA()
      {
          echo('a');
      }
      function writeB()
      {
          echo('b');
      }
  }

  $id   = 1;
  $x    = new _main();
  $x->request($id);

?>

For some of you it may seem weird, but I don't want to have that much lines of code with case and break. For me, they are just making code more difficult to read.

(by the way, writing it 100 times will not making it fun for me too).


CONCLUSION

  • What could be the best,fast and comfortable method?

  • Can I store functions to array and then call them?

  • And will it affect performance? Will be Swicth() even faster?

Thank you :)

EDIT

Perhaps there is a different way of thinking/coding and not only array/switch thing.

like image 254
Max Maximilian Avatar asked Dec 12 '25 04:12

Max Maximilian


1 Answers

I can't say I would ever recommend this but if you really want that many methods within a single class and a singular function to route the calls through...

<?php

class MyClass 
{
    public $id;

    public function callFunction()
    {
        $funcName = 'execute' . $this->id;
        return $this->$funcName();
    }

    private function execute1()
    {
        echo 'execute1() Called.';
    }

    private function execute2()
    {
        echo 'execute2() Called.';
    }
}

$c = new MyClass();
$c->id = 1;
$c->callFunction();

Output:

execute1() Called.

I feel like there is most likely another way to approach this with more information utilising Interfaces and Abstract classes, but with the information to go off the above might suffice your requirement.


Edit: Sadly I don't have the time right now to come up with a detailed solution, and I don't really have enough information to go off but perhaps utilising interfaces is your best solution for your requirement. Below is a very quick example.

<?php

interface WritableInterface
{
    public function write($data);
}

class VersionOneWriter implements WritableInterface
{
    public function write($data)
    {
        return $data . '<br/>';
    }
}

class VersionTwoWriter implements WritableInterface
{
    public function write($data)
    {
        return $data . $data . '<br/>';
    }
}

class MyMainClass
{
    public function request(WritableInterface $writer, $data)
    {
        return $writer->write($data);
    }
}

$c  = new MyMainClass();
$w1 = new VersionOneWriter();
$w2 = new VersionTwoWriter();

echo $c->request($w1, 'DataString');
echo $c->request($w2, 'DataString');

Essentially when you call your request function you pass along a Writer class which implements the WritableInterface. Anything that implements that interface has to have a write() method.

Now when you pass your data across with your method, since you are also passing a writer along that can handle the data you can safely call ->write($data) within your request() method and the result will be dependent on the class you passed through.

If you ever need another method of writing you can just add create another class that implements your interface

Hopefully that made some sense, it was a bit of a ramble as I have to disappear for a bit. If you have any questions I'll try to check back when I have time.

--

Edit2: The define() in this instance requires PHP7+ since I'm defining an array, but you could prior to PHP7 you could just use a standard array. $classMap = ['FirstClass', 'SecondClass'];

interface MyInterface {}
class FirstClass implements MyInterface {}
class SecondClass implements MyInterface {}

$requestParam = 1;

define('CLASS_MAP', array(
    'FirstClass',
    'SecondClass',
));

$classMap = CLASS_MAP[$requestParam]; // SecondClass

$class = new $classMap;
var_dump($class); // Dumps out: object(SecondClass)#1 (0) {}
like image 101
Mikey Avatar answered Dec 13 '25 20:12

Mikey