PHP 是否具有对象的简写语法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/455800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:48:42  来源:igfitidea点击:

Does PHP feature short hand syntax for objects?

phpobject

提问by Pim Jager

In javascript you can easily create objects and Arrays like so:

在 javascript 中,您可以轻松地创建对象和数组,如下所示:

var aObject = { foo:'bla', bar:2 };
var anArray = ['foo', 'bar', 2];

Are similar things possible in PHP?
I know that you can easily create an array using the array function, that hardly is more work then the javascript syntax, but is there a similar syntax for creating objects? Or should I just use associative arrays?

类似的事情在 PHP 中是可能的吗?
我知道您可以使用 array 函数轻松创建一个数组,这几乎没有 javascript 语法更多的工作,但是是否有类似的语法来创建对象?或者我应该只使用关联数组?

$anArray = array('foo', 'bar', 2);
$anObjectLikeAssociativeArray = array('foo'=>'bla',
                                      'bar'=>2);

So to summarize:
Does PHP have javascript like object creation or should I just use associative arrays?

所以总结一下:
PHP 是否有像对象创建这样的 javascript 或者我应该只使用关联数组?

回答by Crescent Fresh

For simple objects, you can use the associative array syntax and casting to get an object:

对于简单的对象,您可以使用关联数组语法和强制转换来获取对象:

<?php
$obj = (object)array('foo' => 'bar');
echo $obj->foo; // yields "bar"

But looking at that you can easily see how useless it is (you would just leave it as an associative array if your structure was that simple).

但是看着它,你可以很容易地看出它是多么无用(如果你的结构那么简单,你可以把它作为一个关联数组)。

回答by Gumbo

There was a proposal to implement this array syntax. But it was declined.

有人提议实现这种数组语法但被拒绝了。



Update????The shortened syntax for arrays has been rediscussed, accepted, and is now on the way be released with PHP 5.4.

更新???? 数组的缩短语法已经过重新讨论和接受,现在正在与 PHP 5.4 一起发布

But there still is no shorthand for objects. You will probably need to explicitly cast to object:

但是仍然没有对象的简写。您可能需要显式转换为object

$obj = (object) ['foo'=>'bla', 'bar'=>2];

回答by Matthew

As of PHP 5.4, you can do this:

从 PHP 5.4 开始,您可以这样做:

$array = ['foo'=>'bla', 'bar'=>2];

It's not much shorter, but you'll appreciate it if you need to use a lot of hard coded nested arrays (which isn't altogether uncommon).

它不会更短,但是如果您需要使用大量硬编码的嵌套数组(这并不少见),您会很感激。

If you want an object, you would still need to cast each array:

如果你想要一个对象,你仍然需要转换每个数组:

$object = (object) ['foo'=>'bla', 'bar'=>2];

回答by Sanket Sahu

According to the new PHP syntaxes,

根据新的 PHP 语法,

You can use

您可以使用

$array = [1,2,3];

And for associative arrays

对于关联数组

$array = ['name'=>'Sanket','age'=>22];

For objects you can typecast the array to object

对于对象,您可以将数组类型转换为对象

$object = (object)['name'=>'Sanket','age'=>22];

回答by Justin Poliey

There is no object shorthand in PHP, but you can use Javascript's exact syntax, provided you use the json_encodeand json_decodefunctions.

PHP 中没有对象速记,但您可以使用 Javascript 的确切语法,前提是您使用json_encodejson_decode函数。

回答by Ronald Conco

The method provided by crescentfresh works very well but I had a problem appending more properties to the object. to get around this problem I implemented the spl ArrayObject.

crescentfresh 提供的方法效果很好,但是我在向对象附加更多属性时遇到了问题。为了解决这个问题,我实现了 spl ArrayObject。

class ObjectParameter extends ArrayObject  {
     public function  __set($name,  $value) {
        $this->$name = $value;
    }

    public function  __get($name) {
      return $this[$name];
    }
}

//creating a new Array object
$objParam = new ObjectParameter;
//adding properties
$objParam->strName = "foo";
//print name
printf($objParam->strName);
//add another property
$objParam->strUser = "bar";

There is a lot you can do with this approach to make it easy for you to create objects even from arrays, hope this helps .

您可以使用这种方法做很多事情,使您甚至可以轻松地从数组创建对象,希望这会有所帮助。

回答by James Hornitzky

Like the json_decode idea, wrote this:

就像 json_decode 的想法,这样写:

function a($json) {
 return json_decode($json, true); //turn true to false to use objets instead of associative arrays
}

//EXAMPLE
$cat = 'meow';

$array = a('{"value1":"Tester", 
  "value2":"'.$cat.'", 
  "value3":{"valueX":"Hi"}}');

print_r($array);