search
Homephp教程PHP开发php面向对象编程之对象

前面我们知道了什么叫类,以及知道了类有成员属性和成员方法,这一篇我们来认识一下对象。


对象是面向对象编程的单位,通过类可以实例化出许多对象,创建类的意义也就在于此。创建一个对象很简单,只需使用new关键字并在其后加上一个和类名同名的方法即可。对象的实例化格式如下:

$变量名=new 类名([参数])

说明:

“$变量名”是类创建的对象的引用名称,通过它可以访问对象的成员属性和成员方法。

“new ”表示要创建一个新的对象。

“参数”指定了类的构造方法用于初始化对象的值。


在前面已经声明了一个Person类,如下

<?php
    class Person{
            public $name;   
            public $age;   
            public $sex;   
                                                                                                                                     
                                                                                                                                             
        public function say(){
            echo "这个人在说话";   
        }
                                                                                                                                     
        public function run(){
            echo "这个人在走路";  
        }
        public function eat($food){
            echo "这个人在吃".$food;  
        }
                                                                                                                                                 
    }
?>

使用这个类来创建对象

$Person1 = new Person();//创建了第一个Person类的对象,引用名为$Person1
$Person2 = new Person();//创建了第二个Person类的对象,引用名为$Person2

。。。

一个类可以创建许多独立的对象,上面只创建了$Person1和$Person2两个对象,相当于在内存中开辟了两个空间用于存放这两个对象。


对象中成员的访问

上面已经这实例化出两个对象$Person1和$Person2,现在给它们的成员$name分别赋予初值

$Person1->name="张三";
$Person2->name="李四;

访问对象中的成员属性$name

echo "$Person1对象的名字是:".$Person1->name."<br>;//$Person1对象的名字是张三
echo "$Person2对象的名字是:".$Person2->name."<br>;//$Person2对象的名字是李四

访问对象中的成员方法say()

$Person1->say();
$Person2->say();


 


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 Article

Hot Tools

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version