search
Homephp教程PHP开发php面向对象编程之类的声明

写在前言:这段时间在看windframework框架,感觉它的面向对象的特性较强,虽然之前我对PHP面向对象编程有一定的基础,但现在看起来似乎有点力不从心的感觉,所以我觉得很有必要系统地整理一下关于PHP面向对象编程的知识点,以便以后翻阅,同时也检查一下自己在这方面欠缺的地方,以便补上。现在就从最简单的开始,把PHP面向对象编程的知识点从头到尾走一遍。

php面向对象编程之类的声明

类是表明如何创建对象的一组指令,但它还不是一个对象,而只是对象的一个描述,它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和方法两个主要部分。在PHP面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属性说明和方法说明两个主要部分。


PHP类的声明很简单,只需使用一个关键字 class 开头,后面跟着一个自定义的类别名称,类别名称可以是任何非 PHP 保留字的名字。后面跟着一对花括号,里面包含有类成员和方法的定义。另外,有时在class关键字的前面也需要加一些修饰类的关键字,如abstract或着final等。


类的声明格式
[修饰类的关键字] class 类名{
  类的成员:
  类的方法:
}
下面创建一个名为Person的类:

<?php
class Person{
                                    
    //下面是人的成员属性
    var $name;  //人的名字
    var $sex;   //人的性别
    var $age;   //人的年龄
                                        
    //下面是人的成员方法
    function say(){ //这个人可以说话的方法
                                        
        echo "这个人在说话";
    }
                                        
    function run(){ //这个人可以走路的方法
                                        
        echo "这个人在走路";
    }
}
?>


  

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools