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

What does class in php mean?

Oct 19, 2019 pm 05:48 PM
php kind

What does class in php mean?

In PHP, a class (Class) is a collection of variables and functions that act on these variables. It is a collection of objects with the same attributes and operations. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: attributes and operations.

PHP is a loosely typed language, so overloading by type does not work, nor does overloading by different numbers of parameters. Sometimes it's good to overload constructors in an orientation so that you can create objects in different ways (passing different numbers of arguments). In PHP, this is achieved through classes.

In PHP, information encapsulation is completed through classes. The syntax for defining a class in PHP is:

<?php
class Class_name       // 在面向对象编程类中,习惯上类的第一个字符为大写,并且必须符合变量的命名规则。
{
//函数与变量的集合
}
?>
Copy after login

When defining a class, you can define it in the format of your own preference, but It is best to maintain a standard so that development will be more efficient.
Data members are defined using the "var" declaration in the class. Before assigning values ​​to the data members, they have no type. A data member can be an integer, an array, an associated array (Associative Array) or an object.
The following is a practical example of a class definition:

<?php 
class Student 
{
var $str_Name; //姓名
var $str_Sex; //性别
var $int_Id; //学号
var $int_English;  //英语成绩
var $int_maths; //数学成绩
}
?>
Copy after login

This is a simple example of a very common class definition, used to display students’ academic performance. The class name is Student, and the Student class covers it A student's basic attributes: name, gender, student number, English score and math score.

function We call it a function defined in a class. When accessing class member variables in a function, you should use $this->var_name, where var_name refers to the variable declared in the class. Otherwise, for a function, it can only be a local variable. We first define an Input() function to assign an initial value to the object in the instance:

function  Input ( $Name, $Sex, $Id, $Englis, $Maths)
{
         $this->str_Name=$Name;
         $this->str_Sex =$Sex;
         $this->int_Id =$Id;
         $this->int_Englis=$English;
$this->int_Maths=$Maths;
       }
Copy after login

Now we define a function called "ShowInfo()" to print students The basic situation:

function ShowInfo()  //定义ShowInfo()函数
    {
echo (“姓名:$this->str_Name<br> 
”);
echo (“性别:$this->str_Sex <br> 
”);
echo (“学号:$this->int_Id <br> 
”);
echo (“英语成绩:$this->int_English <br> 
”);
echo (“数学成绩:$this->int_Maths <br> 
”);
    }
Copy after login

The defined class must use the new keyword to generate the object:

$A_student=new Student;
Copy after login

For example, we want to create an instance for an object named $Wing, And for assignment, you can use the following code:

$Wing =new Student;  //用new关键词来生成对象
$Wing ->Input (“Wing”,”男”,33,95,87); 
//分别输入Wing的姓名、性别、学号、英语成绩、数学成绩,其中姓名和性别是字符型变量,所以需要用双引号,其它为数值型变量则不需要。
Copy after login

Through the complete source code below, we can clearly see how classes are used in PHP:

<?php 
class Student 
{
var $str_Name; 
var $str_Sex; 
var $int_Id; 
var $int_English;  
var $int_maths; 
    function  Input ( $Name, $Sex, $Id, $English, $Maths)
       {
         $this->str_Name=$Name;
         $this->str_Sex =$Sex;
         $this->int_Id =$Id;
         $this->int_English=$English;
$this->int_Maths=$Maths;
} 
function ShowInfo()  
    {
echo (“姓名:$this->str_Name<br> 
”);
echo (“性别:$this->str_Sex <br> 
”);
echo (“学号:$this->int_Id <br> 
”);
echo (“英语成绩:$this->int_English <br> 
”);
echo (“数学成绩:$this->int_Maths <br> 
”);
    }
   } 
  $Wing = new Student;
  $Wing->Input (“Wing”,”男”,33,95,87);
  $Paladin = new Student;
  $Paladin->Input (“paladin”,”女”,38,58,59.5);
$Wing->ShowInfo();
$Paladin->ShowInfo();
?>
Copy after login

The execution result should be like this:

姓名:Wing
性别:男 
学号:33 
英语成绩:95 
数学成绩:87 
姓名:Paladin
性别:女 
学号:38 
英语成绩:58 
数学成绩:59.5
Copy after login

The existing version of PHP has greatly improved its support for object-oriented programming compared with previous versions, but it supports It is not complete yet, but the support provided by PHP for object-oriented programming languages ​​at this stage is not only helpful for us to design the structure of the program, but also provides great help for the maintenance of the program.

For more PHP related knowledge, please visit php中文网!

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles