簡體   English   中英

PHP強制函數調用

[英]PHP mandatory function call

我知道可以使用接口來強制執行函數的定義,但是我找不到能使用戶強制執行函數調用的東西,例如,如果我創建一個類而成為另一個類的成員(通過擴展等),則使用一個函數,以便該類自動確保強制性函數隨該函數一起部分調用。

我的意思是,進一步澄清:

class domain {

   function isEmpty($input) {
      //apply conditional logic and results
   }
}
class test extends domain {

   function addTestToDBTable($test) {
      /** 
       *  try to add but this class automatically makes it so that all rules of
       *  class domain must be passed before it can run
       *  - so essentially, I am no longer required to call those tests for each and
       *    every method 
       **/  
   }
 }

抱歉,無論如何,這似乎是不連貫的。 當然,這似乎很懶,但是我希望能夠強制上下文而不必擔心

更新:

好的,進一步澄清一下:在PHP中,如果我為子類擴展並聲明__construct(),則該子類將覆蓋父__construct()。 我不希望這樣,我希望保留父構造並根據孩子的意願隨意執行任務,就像子類也可以這樣做一樣。

我想可以用兩種不同的方式來完成。

面向方面的編程

在這里看看https://github.com/AOP-PHP/AOP

生成或編寫代理類

一個非常簡單的示例可能是:

<?php
class A {
    public function callMe() {
        echo __METHOD__ . "\n";
    }
}

class B extends A {

    // prevents instantiation
    public function __construct() {
    }

    public function shouldCallMe() {
        echo __METHOD__ . "\n";
    }

    public static function newInstance() {
        return new ABProxy();
    }
}

class ABProxy {
    private $b;

    public function __construct() {
        $this->b = new B();
    }

    public function __call($method, $args) {
        $this->b->callMe();
        return call_user_func_array(array($this->b, $method), $args);
    }
}

// make the call
$b = B::newInstance();
$b->shouldCallMe();

// Outputs
// ------------------
// A::callMe
// B::shouldCallMe

希望這會有所幫助。

聽起來好像您想要裝飾器

有關如何執行此操作的詳細說明,請參見此答案 請注意,它不需要類擴展。

我將使用帶有某些文檔塊元編程魔術的域驗證裝飾器。 但這確實是整個圖書館的工作,這無疑是存在的。

小提琴

<?php
class FooDomain {
    public static function is_not_empty($input) {
        return !empty($input);
    }
}

class Foo {
    /**
     * @domain FooDomain::is_not_empty my_string
     */
    public function print_string($my_string) {
        echo $my_string . PHP_EOL;
    }
}

$foo = new DomainValidator(new Foo());
$foo->print_string('Hello, world!');
try {
    $foo->print_string(''); // throws a DomainException
} catch (\DomainException $e) {
    echo 'Could not print an empty string...' . PHP_EOL;
}

// ---

class DomainValidator {
    const DOMAIN_TAG = '@domain';

    private $object;

    public function __construct($object) {
        $this->object = $object;
    }

    public function __call($function, $arguments) {
        if (!$this->verify_domain($function, $arguments)) {
            throw new \DomainException('Bad domain!');
        }

        return call_user_func_array(
            array($this->object, $function),
            $arguments
        );
    }

    public function __get($name) {
        return $this->object->name;
    }

    public function __set($name, $value) {
        $this->object->name = $value;
    }

    private function verify_domain($function, $arguments) {
        // Get reference to method
        $method = new \ReflectionMethod($this->object, $function);
        $domains = $this->get_domains($method->getDocComment());
        $arguments = $this->parse_arguments(
            $method->getParameters(),
            $arguments
        );
        foreach ($domains as $domain) {
            if (!call_user_func(
                $domain['name'],
                $arguments[$domain['parameter']]
            )) {
                return false;
            }
        }
        return true;
    }

    private function get_domains($doc_block) {
        $lines = explode("\n", $doc_block);
        $domains = array();
        $domain_tag = DomainValidator::DOMAIN_TAG . ' ';
        foreach ($lines as $line) {
            $has_domain = stristr($line, $domain_tag) !== false;
            if ($has_domain) {
                $domain_info = explode($domain_tag, $line);
                $domain_info = explode(' ', $domain_info[1]);
                $domains[] = array(
                    'name'      => $domain_info[0],
                    'parameter' => $domain_info[1],
                );
            }
        }
        return $domains;
    }

    private function parse_arguments($parameters, $values) {
        $ret = array();
        for ($i = 0, $size = sizeof($values); $i < $size; $i++) {
            $ret[$parameters[$i]->name] = $values[$i];
        }
        return $ret;
    }
}

輸出:

Hello, world!
Could not print an empty string...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM