开发者

Is there a magic class function that can get a class constructor to take arguments like an associative array?

开发者 https://www.devze.com 2023-03-10 00:53 出处:网络
I have some code that looks something like this: <? $addr = array(\"city\" => $city, \"streetname\" => $streetname,

I have some code that looks something like this:

<?
    $addr = array("city" => $city, 
        "streetname" => $streetname,
 开发者_JAVA技巧       "housenumber" => $housenumber);

    $address = new address($addr);
?>

This just seems slightly redundant to me. Could I write the address to class to take this constructor?

<?
    $address = new address("city" => $city, 
        "streetname" => $streetname,
        "housenumber" => $housenumber);
?>

(Yes I know I could create the array between the parenthesis of the constructor, I'm just curious)


That is reminiscent of an idea called named parameters (seen in Python, Objective-C and C#), which PHP unfortunately doesn't support, nor do its developers plan to add support for anytime.

0

精彩评论

暂无评论...
验证码 换一张
取 消