Home >Backend Development >PHP Problem >How to declare an object in php

How to declare an object in php

尚
Original
2019-10-28 17:16:269588browse

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( &#39;https://www.php.cn/&#39; );
$taobao->setUrl( &#39;www.taobao.com&#39; );
$google->setUrl( &#39;www.google.com&#39; );

// 调用成员函数,获取标题和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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn