Home >Backend Development >PHP Problem >How to declare an object in php
To create a new object object in PHP, use the new statement to instantiate a class:
<?php class foo { function do_foo() { echo "Doing foo."; } } $bar = new foo; $bar->do_foo(); ?>
After instantiating the object, we can use the object to call members Method, the member method of this object can only operate the member variables of this object:
// 调用成员函数,设置标题和URL $runoob->setTitle( "php中文网" ); $taobao->setTitle( "淘宝" ); $google->setTitle( "Google 搜索" ); $runoob->setUrl( 'https://www.php.cn/' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 调用成员函数,获取标题和URL $runoob->getTitle(); $taobao->getTitle(); $google->getTitle(); $runoob->getUrl(); $taobao->getUrl(); $google->getUrl();
Recommended: php server
The above is the detailed content of How to declare an object in php. For more information, please follow other related articles on the PHP Chinese website!