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

How to declare an object in php

Release: 2023-02-27 21:42:01
Original
9589 people have browsed it

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();
?>
Copy after login

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template