How to create a new object in php

藏色散人
Release: 2023-02-25 22:54:02
Original
3644 people have browsed it

How to create a new object in php

How to create a new object in php?

First define a class;

Then after the class is created, we can use the new operator to instantiate objects of this class;

The complete code is as follows:

<?php 
class Site { 
  /* 成员变量 */ 
  var $url; 
  var $title; 
   
  /* 成员函数 */ 
  function setUrl($par){ 
     $this->url = $par; 
  } 
   
  function getUrl(){ 
     echo $this->url . PHP_EOL; 
  } 
   
  function setTitle($par){ 
     $this->title = $par; 
  } 
   
  function getTitle(){ 
     echo $this->title . PHP_EOL; 
  } 
} 

$php = new Site; 
$taobao = new Site; 
$google = new Site; 

// 调用成员函数,设置标题和URL 
$php->setTitle( "php中文网" ); 
$taobao->setTitle( "淘宝" ); 
$google->setTitle( "Google 搜索" ); 

$php->setUrl( &#39;www.php.cn&#39; ); 
$taobao->setUrl( &#39;www.taobao.com&#39; ); 
$google->setUrl( &#39;www.google.com&#39; ); 

// 调用成员函数,获取标题和URL 
$php->getTitle(); 
$taobao->getTitle(); 
$google->getTitle(); 

$php->getUrl(); 
$taobao->getUrl(); 
$google->getUrl(); 
?>
Copy after login

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to create a new 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