PHP类与对象,构造函数和析构函数,加上this绑定详解
一、类与对象
不仅在PHP中,在所有面向对象程序设计语言中,类都是抽象的,对象是类的一个实例。所有抽象就是:”抽取出相像的”。
在生活中,人是一个类,抽出相像的是:人都有五官,头发,手脚,会吃饭睡觉等等。
看一下PHP中的类与对象代码的实现
<?php class Person{ //定义一个叫Person的类 public $name; //声明共有的属性:名字 public function say(){ //声明共有的方法:说话 echo 'saying'; } } ?>
构造函数和析构函数
构造函数是在new一个对象的时候执行的方法,析构函数是对象被销毁的时候执行的方法。
对象的销毁可以显式销毁或者等代码页面执行完毕后自动销毁。
<?php class Person{ //定义一个叫Person的类 public $name; //声明共有的属性:名字 public function say(){ //声明共有的方法:说话 echo 'saying'; } public function __construct(){ //固定的__construct声明构造函数 echo 'construct'; //在对象创建时输出 } public function __destruct(){ echo 'destruct'; //对象销毁时输出 } } $a =new Person(); //输出construct //待代码结束后输出 destruct ?>
this的绑定
this返回当前绑定的对象。在PHP中如果要在方法体中调用对象中的属性,都要加上 this,否则被认为是局部变量。
<?php class Person{ //定义一个叫Person的类 public $name='color'; //声明共有的属性:名字 public function say(){ //声明共有的方法:说话 echo 'saying'; } public function __construct(){ echo $this->name; //输出color,如果是echo $name会报错,因为此时的$name未定义 } } $a=new Person(); ?>
本文作者: By: 罗坚元

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

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov
