php 根据$variable值示例化特定的类对象,而不使用条件

wfypjpf4  于 2022-10-30  发布在  PHP
关注(0)|答案(4)|浏览(121)

我想写下面的代码,不带switch,if else或三元运算符。代码如下所示:

switch (type){
  case 'bed':
     function create{
      return new Bed($customer, $price, $dimensions );
     }
      break;
  case 'desk':
     function create{
      return new Desk($customer, $price, $dimensions);
    }
      break;

  case 'chair':
       function create{
      return new Chair($customer, $price, $dimensions, $type );
   }
      break;

  }
a14dhokn

a14dhokn1#

如果你想在这里调用另一个类,你可以把你想要的类设置为全局的。然后在下面的函数中,你可以在那里使用global来调用它们。
输出示例:

<?php
  ini_set('display_errors', 1);
  ini_set('display_startup_errors', 1);
  error_reporting(E_ALL);

  class Bed{
    function __construct($customer, $price, $dimensions){
      echo "Ordered <b>Bed</b> for: <b>$customer</b> at the price of: <b>$price</b> and with the dimensions of: <b>$dimensions</b>";
    }
  }
  class Chair{
    function __construct($customer, $price, $dimensions){
      echo "Ordered <b>Chair</b> for: <b>$customer</b> at the price of: <b>$price</b> and with the dimensions of: <b>$dimensions</b>";
    }
  }
  class Desk{
    function __construct($customer, $price, $dimensions){
      echo "Ordered <b>Desk</b> for: <b>$customer</b> at the price of: <b>$price</b> and with the dimensions of: <b>$dimensions</b>";
    }
  }

  class Furniture{

    private function bed($customer, $price, $dimensions){
      new Bed($customer, $price , $dimensions);
    }

    private function chair($customer, $price, $dimensions){
      new Chair($customer, $price , $dimensions);
    }

    private function desk($customer, $price, $dimensions){
      new Desk($tcustomer, $price , $dimensions);
    }

    function __construct($type = null, $customer = null, $price = null, $dimensions = null) {
      return $this->$type($customer, $price, $dimensions);
    }
  }

  new Furniture('bed','Joes Momma','$1.99','4x5x2');
atmip9wb

atmip9wb2#

你可以在if else语句中使用相同的代码,如下所示。if ==不起作用,尝试添加===代替

if(type == "bed"){

     function create{
      return new Bed($customer, $price, $dimensions );
     }}
else if(type == "desk"){

     function create{
      return new Desk($customer, $price, $dimensions);
    }}
    else if(type == "chair"){

       function create{
      return new Chair($customer, $price, $dimensions, $type );
   }
  }
nkoocmlb

nkoocmlb3#

您可以使用储存在$type中的类别名称,传回类别的新执行严修,让程式码更简洁。若要以不同的参数呼叫建构函式,请使用splat运算子,将参数收集在数组中。

<?php

function create($type){
    global $customer, $price, $dimensions;
    $className = ucfirst($type);
    $params = [$customer, $price, $dimensions, $type];
    return new $className(...$params);
}

create($type);
pgvzfuti

pgvzfuti4#

  • 📎 -看起来您正在尝试编写一个工厂函数。*

您可以创建一个Map到类创建器函数的允许 * 类型 * 列表,而不是使用条件。

enum FurnitureType: string {
    case Bed = 'bed';
    case Desk = 'desk';
    case Chair = 'chair';
}

class FurnitureFactory {
    private $creators;

    public function __construct() {
        // ideally this would be static but PHP no-likey
        $this->creators = [
            FurnitureType::Bed->value => static fn($customer, $price, $dimensions) =>
                new Bed($customer, $price, $dimensions),
            FurnitureType::Desk->value => static fn($customer, $price, $dimensions) =>
                new Desk($customer, $price, $dimensions),
            FurnitureType::Chair->value => static fn($customer, $price, $dimensions) =>
                new Chair($customer, $price, $dimensions, FurnitureType::Chair->value),
        ];
    }

    public function creator(FurnitureType $type, $customer, $price, $dimensions) {
        $creator = $this->creators[$type->value] ??
            throw new Exception("Not implemented for type {$type->name}");
        return fn() => $creator($customer, $price, $dimensions);
    }
}

$factory = new FurnitureFactory();
$bedCreator = $factory->creator(FurnitureType::Bed, 'customer', 100, '200x100');
print_r($bedCreator());

$chairCreator = $factory->creator(FurnitureType::Chair, 'customer', '50', '20x50');
print_r($chairCreator());

需要PHP 8.1.0或更高版本
https://3v4l.org/fDuKG#v8.1.12

相关问题