开发者

Default associative array value in PHP

开发者 https://www.devze.com 2022-12-11 21:50 出处:网络
I\'m just wondering if its possible to have something like this: $image = array( \"default\" => \"test.jpg\",

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

精彩评论

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