Table of Contents
PHP 5.3新增魔术方法__invoke概述,5.3__invoke
php魔术方法
详细说明一下php5中的魔术方法
Home php教程 php手册 PHP 5.3新增魔术方法__invoke概述,5.3__invoke

PHP 5.3新增魔术方法__invoke概述,5.3__invoke

Jun 13, 2016 am 09:28 AM
invoke php php5 method

PHP 5.3新增魔术方法__invoke概述,5.3__invoke

PHP自从5.3版以来就新增了一个叫做__invoke的魔术方法,使用该方法就可以在创建实例后,直接调用对象。如下示例所示:

class testClass
{
public function __invoke
{
print "hello world";
}
}
$n = new testClass;
$n();

Copy after login

执行结果为:

hello world。

Copy after login

php官方示例如下:

class CallableClass
{
public function __invoke($x)
{
var_dump($x);
}
}
$obj = new CallableClass;
$obj(5);
var_dump(is_callable($obj));
Copy after login

附:PHP其他魔术方法详解:

php中的类就可以使用魔术方法了。其规定以两个下划线(__)开头的方法都保留为魔术方法,所以建议大家函数名最好不用__开头,除非是为了重载已有的魔术方法。

The function names __construct, __destruct, __call, __callStatic, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __invoke, __set_state and__clone are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.

如下这两个方法是为在类和他们的父类中没有声明的属性而设计的。
__get($property)当调用一个未定义的属性时,此方法会被触发,传递的参数是被访问的属性名。
__set($property,$value)给一个未定义的属性赋值时,此方法会被触发,传递的参数是被设置的属性名和值。
这里的没有声明包括当使用对象调用时,访问控制为proteced,private的属性(即没有权限访问的属性)。
与__get方法和__set方法相同,这里的没有声明包括当使用对象调用时,访问控制为proteced,private的属性(即没有权限访问的属性)。

这里的未定义的方法包括没有权限访问的方法;如果方法不存在就去父类中找这个方法,如果父类中也不存在就去调用本类的__call()方法,如果本类中不存在__call()方法就去找父类中的__call()方法

__autoload函数,它会在试图使用尚未被定义的类时自动调用。通过调用此函数,脚本引擎在php出错失败前有了最后一个机会加载所需的类。

注意:在__autoload函数中抛出的异常不能被catch语句块捕获并导致致命错误,所以应该在函数本身做捕获。

__construct构造方法,当一个对象创建时调用此方法,相对于php4使用此方法的好处是:可以使构造方法有一个独一无二的名称,无论它所在的类的名称是什么.这样你在改变类的名称时,就不需要改变构造方法的名称。

__destruct析构方法,php将在对象被销毁前(即从内存中清除前)调用这个方法。默认情况下,php仅仅释放对象属性所占用的内存并销毁对象相关的资源,析构函数允许你在使用一个对象之后执行任意代码来清除内存。当php决定你的脚本不再与对象相关时,析构函数将被调用。

在一个函数的命名空间内,这会发生在函数return的时候。对于全局变量,这发生于脚本结束的时候。如果你想明确地销毁一个对象,你可以给指向该对象的变量分配任何其它值.通常将变量赋值为null或者调用unset。

php5中的对象赋值是使用的引用赋值,如果想复制一个对象则需要使用clone方法,在调用此方法是对象会自动调用__clone魔术方法,如果在对象复制需要执行某些初始化操作,可以在__clone方法实现。

__tostring方法在将一个对象转化成字符串时自动调用,比如使用echo打印对象时。

在php5.2.0之前,__tostring方法只有结合使用echo()或print()时才能生效。php5.2.0之后,则可以在任何字符串环境生效(例如通过printf(),使用%s修饰符),但不能用于非字符串环境(如使用%d修饰符)。从php5.2.0,如果将一个未定义__tostring方法的对象转换为字符串,会报出一个e_recoverable_error错误。

__wakeup反串行化的时候调用

serialize()检查类中是否有魔术名称__sleep的函数。如果这样,该函数将在任何序列化之前运行。它可以清除对象并应该返回一个包含有该对象中应被序列化的所有变量名的数组。

使用__sleep的目的是关闭对象可能具有的任何数据库连接,提交等待中的数据或进行类似的清除任务。此外,如果有非常大的对象而并不需要完全储存下来时此函数也很有用。

PHP相反地,unserialize()检查具有魔术名称__wakeup的函数的存在。如果存在,此函数可以重建对象可能具有的任何资源。使用__wakeup的目的是重建在序列化中可能丢失的任何数据库连接以及处理其它重新初始化的任务。
当尝试以调用函数的方式调用一个对象时,__invoke方法会被自动调用。

它的工作方式类似于__call()魔术方法,__callstatic()是为了处理静态方法调用。

php确实加强了对__callstatic()方法的定义;它必须是公共的,并且必须被声明为静态的。同样,__call()魔术方法必须被定义为公共的,所有其他魔术方法都必须如此。

php魔术方法

这样绕的话是为了安全,也就是面向对象里封装的理念,如果直接设置Public那么没个对象都可以自由设置这个属性的值了,而且没有经过任务逻辑判断,这样绕弯的话可以增加安全性
 

详细说明一下php5中的魔术方法

PHP处理对象部分的内核完全重新开发过,提供更多功能的同时也提高了性能。在以前版本的php中,处理对象和处理基本类型(数字,字符串)的方式是一样的。这种方式的缺陷是:当将对象赋值给一个变量时,或者通过参数传递对象时,对象将被完全拷贝一份。在新的版本里,上述操作将传递引用(可以把引用理解成对象的标识符),而非值。
很多PHP程序员可能甚至没有察觉到老的对象处理方式。事实上,大多数的php应用都可以很好地运行。或者仅仅需要很少的改动。
私有和受保护成员
PHP5引入了私有和受保护成员变量的概念。我们可以用它来定义类成员的可见性。
例子
受保护成员可以被子类访问, 而私有成员只能被类本身访问。

代码:--------------------------------------------------------------------------------

class MyClass {
private $Hello = "Hello, World!\n";
protected $Bar = "Hello, Foo!\n";
protected $Foo = "Hello, Bar!\n";

function printHello() {
print "MyClass::printHello() " . $this->Hello;
print "MyClass::printHello() " . $this->Bar;
print "MyClass::printHello() " . $this->Foo;
}
}

class MyClass2 extends MyClass {
protected $Foo;

function printHello() {
MyClass::printHello(); /* Should print */
print "MyClass2::printHello() " . $this->Hello; /* Shouldn't print out anything */
print "MyClass2::printHello() " . $this->Bar; /* Shouldn't print (not declared)*/
print "MyClass2::printHello() " . $this->Foo; /* Should print */
}
}

$obj = new MyClass();
print $obj->Hello; /* Shouldn't print out anything */
print $obj->Bar; /* Shouldn't print out anything */
print $obj->Foo; /* Should......余下全文>>
 

Statement of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles