Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use class in php

Shulou Source: shulou.com Published: 2022-06-02 07:56:32 10月25日 Update

This article mainly explains "how to use class in php", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "how to use class in php"!

class in php is the keyword used to declare a class. The syntax for defining a class in php is usually in the form of "class phpClass {var $var1;function myfunc ($arg1, $arg2) {...}}." "。

Operating environment: Windows 7, PHP 7.4, DELL G3

What does class mean in PHP?

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

PHP class definitions

PHP defines classes in the following syntax:

The analysis is as follows:

Classes are defined using the class keyword followed by the class name.

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

The variables of a class are declared using var, and variables can also be initialized.

Function definitions are similar to those of PHP functions, but functions can only be accessed through the class and the objects it instantiates.

examples

The variable $this represents its own object.

PHP_EOL is a newline.

Creating objects in PHP

After a class is created, we can instantiate objects of that class using the new operator:

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

In the above code, we created three objects, each of which is independent. Next, let's see how to access member methods and member variables.

invoke member method

After instantiating an object, we can use that object to call member methods that can only operate on member variables of that object:

//Call member function to set title and URL$runoob->setTitle( "rookie tutorial" );$taobao->setTitle( "Taobao" );$google->setTitle( "Google Search" );$runoob-> setUrl ('www.example.com');$taobao->setUrl www.taobao.com;$google->setUrl www.google.com;/Call member function to get title and URL$runoob->getTitle();$google-> getTitle ();$runoob->getUrl();$taobao->getUrl ();$google->getUrl(); www.runoob.com

The complete code is as follows:

examples

Executing the above code, the output is:

Rookie tutorial Taobao Google search www.runoob.comwww.taobao.comwww.google.com Thank you for reading, the above is the "php class how to use" content, after learning this article, I believe everyone on php class how to use this problem has a deeper experience, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Object member function variable instance method title code key keyword tutorial rookie tutorial Taobao rookie learning search three content format class name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL NVidia macOS vpn