开发者

Default associative array value in PHP

I'm just wondering if its possible to have something like this:

$image = array(

"default" => "test.jpg",
"width" =>开发者_如何学C 400,
"height" => 500

);

Then you could call:

echo $image // test.jpg
echo $image['width'] // 400

Thanks, Matt Mueller


No, image is an array so it will echo array()

You can however do this with __toString

class image {

    private $defaultImage = 'test.jpg';

    function __toString() {
        return $this->defaultImage;
    }

}

$image = new image;
$image->height = 400;

echo $image; // test.jpg
echo $image->height; //400


Simple answer: No, that's not possible. The only thing that's somewhat similar is using PHP's weak type system and assigning the default value to the array as string until you initialize the array - but I'm not sure that's what you want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜