Home >Backend Development >PHP Problem >What does class in php mean?

What does class in php mean?

藏色散人
藏色散人Original
2021-12-06 10:27:193801browse

Class in php is a keyword used to declare a class. The usual syntax format for defining a class in php is "class phpClass {var $var1; function myfunc ($arg1, $arg2) {...}}" .

What does class in php mean?

The operating environment of this article: Windows 7 system, PHP version 7.4, DELL G3 computer

What does class in php mean?

Class in php is the keyword used to declare a class.

PHP class definition

PHP definition class usually has the following syntax format:

<?php
class phpClass {
  var $var1;
  var $var2 = "constant string";
  
  function myfunc ($arg1, $arg2) {
     [..]
  }
  [..]
}
?>

The analysis is as follows:

Classes use the class keyword Add the class name definition after.

Variables and methods can be defined within a pair of braces ({}) after the class name.

Variables of the class are declared using var, and variables can also be initialized.

Function definition is similar to the definition of PHP function, but the function can only be accessed through the class and its instantiated objects.

Instance

<?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;
  }
}
?>

Variable $this represents the object of itself.

PHP_EOL is the newline character.

Create objects in PHP

After the class is created, we can use the new operator to instantiate objects of this class:

$runoob = new Site;
$taobao = new Site;
$google = new Site;

We have the above code Three objects have been created. Each of the three objects is independent. Next, let's take a look at how to access member methods and member variables.

Call member methods

After instantiating an object, we can use the object to call member methods. The member methods of the object can only operate the member variables of the object:

// 调用成员函数,设置标题和URL
$runoob->setTitle( "菜鸟教程" );
$taobao->setTitle( "淘宝" );
$google->setTitle( "Google 搜索" );
$runoob->setUrl( &#39;www.runoob.com&#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();

The complete code is as follows:

Example

url = $par;
  }
  
  function getUrl(){
     echo $this->url . PHP_EOL;
  }
  
  function setTitle($par){
     $this->title = $par;
  }
  
  function getTitle(){
     echo $this->title . PHP_EOL;
  }
}
$runoob = new Site;
$taobao = new Site;
$google = new Site;
// 调用成员函数,设置标题和URL
$runoob->setTitle( "菜鸟教程" );
$taobao->setTitle( "淘宝" );
$google->setTitle( "Google 搜索" );
$runoob->setUrl( &#39;www.runoob.com&#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();
?>

Execute the above code, the output result is:

菜鸟教程
淘宝
Google 搜索
www.runoob.com
www.taobao.com
www.google.com

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does class in php mean?. 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