简体   繁体   English

PHP中的便捷构造函数

[英]Convenience constructor in PHP

Is there a way of using the __construct function in PHP to create more than one constructor in a hierarchical pattern. 有没有办法在PHP中使用__construct函数以分层模式创建多个构造函数。

For example, I want to create a new instance of my Request class using the constructor 例如,我想使用构造函数创建Request类的新实例

__construct( $rest_noun, $rest_verb, $object_identifier, $additional_arguments );

But I would like a convenience constructor like this: 但我想要一个像这样的便利构造函数:

__construct( $url );

…whereby I can send a URL and the properties are extracted from it. ...我可以发送一个URL,并从中提取属性。 I then call the first constructor sending it the properties I extracted from the URL. 然后我调用第一个构造函数向它发送我从URL中提取的属性。

I guess my implementation would look something like this: 我想我的实现看起来像这样:

function __construct( $rest_noun, $rest_verb, $object_identifier, $additional_arguments )
{
    //
    //  Set all properties
    //

    $this->rest_noun = $rest_noun;
    $this->rest_verb = $rest_verb;
    $this->object_identifier = $object_identifier;
    $this->additional_arguments = $additional_arguments;
}

function __construct( $url )
{
    //
    //  Extract each property from the $url variable.
    //

    $rest_noun = "component from $url";
    $rest_verb = "another component from $url";
    $object_identifier = "diff component from $url";
    $additional_arguments = "remaining components from $url";

    //
    //  Construct a Request based on the extracted components.
    //

    this::__construct( $rest_noun, $rest_verb, $object_identifier, $additional_arguments );
}

…but I'm quite a beginner in PHP so wanted to get your advice on the topic to see if it would work or even if there's a better way to do it. ...但我是PHP的初学者,所以想在这个主题上得到你的建议,看看它是否有用,或者即使有更好的方法。

My guess is if it comes down to it I can always just use a static function for my convenience. 我的猜测是,如果它归结为它我总是可以使用静态函数为我的方便。

Just extends your Request class: 只需扩展您的Request类:

class RequestWithAnotherContructor extends Request
{
        function __construct($url) {
            $rest_noun = "component from $url";
            $rest_verb = "another component from $url";
            $object_identifier = "diff component from $url";
            $additional_arguments = "remaining components from $url";

            // call the parent constructors
            parent::__construct( $rest_noun, $rest_verb, $object_identifier, $additional_arguments );
       }
}

why dont you call the function from the constructor? 为什么不从构造函数调用函数?

function __construct( $url ){
   //stuff you need
   $this->do_first($stuff)
}

function do_first($stuff){
   //stuff is done
}

You could do something with func_get_args as noted in Best way to do multiple constructors in PHP 您可以使用func_get_args执行某些操作,如在PHP中执行多个构造函数的最佳方法中所述

function __construct($param) {
    $params = func_get_args();
    if (count($params)==1) {
        // do first constructor
    } else {
        // do second constructor
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM