首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Objects

Object Initialization

To create a new object, use the new statement to instantiate a class:

代码语言:javascript
复制
<?php
class?foo
{
????function?do_foo()
????{
????????echo?"Doing?foo.";?
????}
}

$bar?=?new?foo;
$bar->do_foo();
?>

For a full discussion, see the Classes and Objects chapter.

Converting to object

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values, with the exception of numeric keys which will be inaccessible unless iterated.

代码语言:javascript
复制
<?php
$obj?=?(object)?array('1'?=>?'foo');
var_dump(isset($obj->{'1'}));?//?outputs?'bool(false)'
var_dump(key($obj));?//?outputs?'int(1)'
?>

For any other value, a member variable named scalar will contain the value.

代码语言:javascript
复制
<?php
$obj?=?(object)?'ciao';
echo?$obj->scalar;??//?outputs?'ciao'
?>

← Iterables

Resources →

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com