This article mainly introduces the object-oriented overloading of PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it.
Definition:
1 ) dynamically "creates" the properties and methods of a class.
2) Achieved through magic method.
3) When calling a property or method of a class that is undefined or invisible in the current environment, the overloaded method will be called.
Attribute overloading
__set 赋值 __get 读取 __isset 判断是否存在 __unset 销毁
Example
// 属性的重载 class Person { public $name = '小芳'; protected $age = 18; public function __get($n) { //echo '试图读取不可访问的属性'.$n; if( $n == 'age'){ return $this -> age; }else{ return '你要查户口吗?'; } } public function __set($n,$v) { //echo '试图设置不可访问的属性','<br/>'; $this -> $n = $v; } public function __isset($n) { echo '判断不可访问的属性'.$n.'是否存在','<br/>'; } public function __unset($n) { echo '销毁不可访问的属性'.$n,'<br/>'; } } $p1 = new Person(); // 读取 //echo $p1 -> age,'<br/>'; //echo $p1 -> xxx,'<br/>'; // 设置 //$p1 -> age = 30; //echo $p1 -> age,'<br/>'; // 判断存在与否 isset($p1 -> age); // 销毁 unset($p1 -> age);
Method overloading
__call 调用不可访问的普通方法 __callStatic 调用不可访问的静态方法
Special note that when __callStatic is defined, it must be defined as a static method.
Example
<?php class MyClass { protected function func($n) { echo '这是一个不可访问的方法'; echo '参数有'.$n; } protected static function fun2() { echo '受保护的静态方法'; } public function __call($function_name,$args) { echo '触发了不可访问的方法'; var_dump($function_name); var_dump($args); } public static function __callStatic($function_name,$args) { echo '触发了不可访问jing tai方法,静态!!!!'; var_dump($function_name); var_dump($args); } } // 实例化 $c1 = new MyClass(); $c1 -> func([1,2,3]); $c1 -> func2([1,2,3]);
Related recommendations:
php object-oriented encapsulation
##php Object-oriented magic methods
php object-oriented static methods, properties and constants
The above is the detailed content of PHP object-oriented overloading. For more information, please follow other related articles on the PHP Chinese website!

深入解读PHP面向对象的封装性封装是面向对象编程的三大特征之一,它是指将数据和对数据的操作封装在一个类中,对外部程序隐藏具体的实现细节,提供对外的接口。在PHP中,通过使用访问修饰符(public、protected、private)来控制属性和方法的可访问性,实现封装的概念。首先,我们来了解一下访问修饰符的作用:public(公开的):公开的属性和方法可以

Go中不支持传统函数重载,但可以通过以下技术模拟:多返回值:方法签名相同但返回类型不同的函数可实现重载。可变参数:使用...语法创建接收可变数量参数的函数,允许处理不同签名的方法调用。

Go语言中不支持函数重载,因为它采用鸭子类型,根据实际类型确定值类型。而多态则通过接口类型和方法调用实现,不同类别的对象可以以相同方式响应。具体来说,Go语言中通过定义接口并实现这些方法,可以使不同类型的对象拥有相似行为,从而支持多态。

如何通过PHP面向对象简单工厂模式实现对象的版本控制和管理在开发大型的、复杂的PHP项目时,版本控制和管理是非常重要的一环。通过适当的设计模式,我们可以更好地管理和控制对象的创建和使用,从而提高代码的可维护性和扩展性。本文将介绍如何使用PHP面向对象简单工厂模式来实现对象的版本控制和管理。简单工厂模式是一种创建类的设计模式,它通过一个工厂类来实例化指定的对象

PHP语言不支持函数重载和函数覆盖,原因是函数重载可能导致二义性。替代方案:使用命名空间隔离函数。设置参数缺省值。使用可变函数参数。

Go语言作为一门静态类型语言,看似不能像动态语言那样实现多态和重载。但是,Go语言利用接口的特性实现了多态,而重载的实现则更加简单和精准。实现多态的方法Go语言中的接口可以在调用过程中实现多态,接口可以描述一个对象的行为,任何实现了接口所有方法的类型都可以称之为该接口类型的实例。通过这种方式,只需定义好接口类型,实现不同的具体类型,就可以实现多态。下面是一个

区别:1、MySQL是关系数据库,NoSQL是非关系型。2、MySQL严格模式限制并不容易扩展,NoSQL容易扩展。3、MySQL创建数据库前需详细的数据库模型,而在NoSQL不需要。4、MySQL提供了大量的报告工具,而nosql没有。5、与MySQL相比,NoSQL提供了更灵活的设计。6、MySQL中使用的标准语言是SQL,而NoSQL中缺乏标准的查询语言。

PHP是一种非常流行的服务器端脚本语言,用于开发Web应用程序。然而,对于一些初学者来说,理解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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

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