PHP中的类-什么叫类(转载)------类入门之捷径_PHP
PHP中的类-什么叫类
Linuxaid 01-03-08 10:16 1594p Wing
--------------------------------------------------------------------------------
在阐述类的概念之前我们来先说说面向对象编程的概念:面向对象的程序设计(Object-Oriented Programming,简记为OOP)立意于创建软件重用代码,具备更好地模拟现实世界环境的能力,这使它被公认为是自上而下编程的优胜者。它通过给程序中加入扩展语句,把函数“封装”进编程所必需的“对象”中。面向对象的编程语言使得复杂的工作条理清晰、编写容易。说它是一场革命,不是对对象本身而言,而是对它们处理工作的能力而言。对象并不与传统程序设计和编程方法兼容,只是部分面向对象反而会使情形更糟。除非整个开发环境都是面向对象的,否则对象产生的好处还没有带来的麻烦多。有人可能会说PHP不是一个真正的面向对象编程的语言, PHP 是一个混合型 语言,你可以使用面向对象编程,也可以使用传统的过程化编程。然而,对于大型项目的开发,你可能想需要在PHP中使用纯的面向对象编程去声明类,而且在你的项目开发只用对象和类。随着项目越来越大,使用面向对象编程可能会有帮助,面向对象编程代码很容易维护,容易理解和重复使用,这些就是软件工程的基础。在基于Web的项目中应用这些概念就成为将来网站成功的关键。
对象(Object)是问题域或实现域中某些事物的一个抽象,它反映此事物在系统中需要保存的信息和发挥的作用;它是一组属性和有权对这些属性进行操作的一组服务的封装体。 关于对象要从两方面理解:一方面指系统所要处理的现实世界中的对象;另一方面对象是计算机不直接处理的对象,而是处理相应的计算机表示,这种计算机表示也称为对象。简单的来说,一个人就是一个对象,一个尺子也可以说是个对象。当这些对象可以用数据直接表示时,我们就称他为属性,尺子的度量单位可以是厘米,公尺或英尺,这个度量单位就是尺子的属性。
在PHP里我们可以定义一个类,类(Class)就是指变量与一些使用这些变量的函数的集合。PHP是一种松散类型的语言,所以通过类型重载不起作用,通过参数的个数不同来重载也不起作用。 有时在面向中重载构造函数非常好,这样你可以通过不同的方法创建对象(传递不同数量的参数)。在PHP中就是通过类来实现的。
在PHP中是通过类来完成信息封装的,在PHP中定义类的语法是:
class Class_name // 在面向对象编程类中,习惯上类的第一个字符为大写,并且必须符合变量的命名规则。
{
//函数与变量的集合
}
?>
在定义类时你可以按自已的喜好的格式进行定义,但最好能保持一种标准,这样开发起来会更有效些。
数据成员在类中使用"var"声明来定义,在给数据成员赋值之前,它们是没有类型的。一个数据成员可以是一个整数,一个数组,一个相关数组(Associative Array)或者是一个对象。
下面是一个类定义的实际例子:
class Student
{
var $str_Name; //姓名
var $str_Sex; //性别
var $int_Id; //学号
var $int_English; //英语成绩
var $int_maths; //数学成绩
}
?>
这是一个很普通定义类的简单例子,用于显示学生的学习成绩,类名为Student,Student类包涵了一个学生的基本属性:姓名、性别、学号、英语成绩和数学成绩。
function我们称之为在类中被定义的函数,在函数中访问类成员变量时,你应该使用$this->var_name,其中var_name指的是类中被声明的变量,否则对一个函数来说,它只能是局部变量。 我们先定义一个Input()的函数,用来给实例中的对象赋以初值:
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;
}
现在我们再定义一个叫“ShowInfo()”的函数,用于打印学生的基本情况:
function ShowInfo() //定义ShowInfo()函数
{
echo (“姓名:$this->str_Name
”);
echo (“性别:$this->str_Sex
”);
echo (“学号:$this->int_Id
”);
echo (“英语成绩:$this->int_English
”);
echo (“数学成绩:$this->int_Maths
”);
}
而定义好的类则必须使用new关键词来生成对象:
$A_student=new Student;
例如我们要为一个名为$Wing的对象创建实例,并进行赋值,可以使用下面的代码:
$Wing =new Student; //用new关键词来生成对象
$Wing ->Input (“Wing”,”男”,33,95,87);
//分别输入Wing的姓名、性别、学号、英语成绩、数学成绩,其中姓名和性别是字符型变量,所以需要用双引号,其它为数值型变量则不需要。
通过下面这段完整的源代码,我们就可以很清楚的看到类在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
”);
echo (“性别:$this->str_Sex
”);
echo (“学号:$this->int_Id
”);
echo (“英语成绩:$this->int_English
”);
echo (“数学成绩:$this->int_Maths
”);
}
}
$Wing = new Student;
$Wing->Input (“Wing”,”男”,33,95,87);
$Paladin = new Student;
$Paladin->Input (“paladin”,”女”,38,58,59.5);
$Wing->ShowInfo();
$Paladin->ShowInfo();
?>
执行结果应是这样的:
姓名:Wing
性别:男
学号:33
英语成绩:95
数学成绩:87
姓名:Paladin
性别:女
学号:38
英语成绩:58
数学成绩:59.5
PHP现有的版本较以前的版本在对面向对象编程的支持方面有了很大的改善,但支持的还不是很完整,不过现阶段PHP对面向对象编程语言提供的支持不但有利于我们设计程序的结构,对于对程序的维护也能提供很大的帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Diffusion can not only imitate better, but also "create". The diffusion model (DiffusionModel) is an image generation model. Compared with the well-known algorithms such as GAN and VAE in the field of AI, the diffusion model takes a different approach. Its main idea is a process of first adding noise to the image and then gradually denoising it. How to denoise and restore the original image is the core part of the algorithm. The final algorithm is able to generate an image from a random noisy image. In recent years, the phenomenal growth of generative AI has enabled many exciting applications in text-to-image generation, video generation, and more. The basic principle behind these generative tools is the concept of diffusion, a special sampling mechanism that overcomes the limitations of previous methods.

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

We know that LLM is trained on large-scale computer clusters using massive data. This site has introduced many methods and technologies used to assist and improve the LLM training process. Today, what we want to share is an article that goes deep into the underlying technology and introduces how to turn a bunch of "bare metals" without even an operating system into a computer cluster for training LLM. This article comes from Imbue, an AI startup that strives to achieve general intelligence by understanding how machines think. Of course, turning a bunch of "bare metal" without an operating system into a computer cluster for training LLM is not an easy process, full of exploration and trial and error, but Imbue finally successfully trained an LLM with 70 billion parameters. and in the process accumulate

Title: A must-read for technical beginners: Difficulty analysis of C language and Python, requiring specific code examples In today's digital age, programming technology has become an increasingly important ability. Whether you want to work in fields such as software development, data analysis, artificial intelligence, or just learn programming out of interest, choosing a suitable programming language is the first step. Among many programming languages, C language and Python are two widely used programming languages, each with its own characteristics. This article will analyze the difficulty levels of C language and Python

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

Retrieval-augmented generation (RAG) is a technique that uses retrieval to boost language models. Specifically, before a language model generates an answer, it retrieves relevant information from an extensive document database and then uses this information to guide the generation process. This technology can greatly improve the accuracy and relevance of content, effectively alleviate the problem of hallucinations, increase the speed of knowledge update, and enhance the traceability of content generation. RAG is undoubtedly one of the most exciting areas of artificial intelligence research. For more details about RAG, please refer to the column article on this site "What are the new developments in RAG, which specializes in making up for the shortcomings of large models?" This review explains it clearly." But RAG is not perfect, and users often encounter some "pain points" when using it. Recently, NVIDIA’s advanced generative AI solution

VSCode (Visual Studio Code) is an open source code editor developed by Microsoft. It has powerful functions and rich plug-in support, making it one of the preferred tools for developers. This article will provide an introductory guide for beginners to help them quickly master the skills of using VSCode. In this article, we will introduce how to install VSCode, basic editing operations, shortcut keys, plug-in installation, etc., and provide readers with specific code examples. 1. Install VSCode first, we need
