


How to use the object-oriented magic method __call function in PHP
PHP uses the __call function of the object-oriented magic method: 1. When an inaccessible member method is called, the [__call] magic method will be called; 2. The member method does not exist and the member method When it is protected or private, the [__call] magic method is called.
PHP uses the __call function of the object-oriented magic method:
Basic introduction:
(1) When we call an inaccessible member method, the __call magic method will be called.
(2) The inaccessible member method refers to (1 . The member method does not exist, 2. The member method is protected or private)
Requirement
We just hope that inaccessible member methods can be called directly outside the class (private, protected).
Case description
<?php header('content-type:text/html;charset=utf-8'); //__call魔术方法 class Monk{ public $name; protected $hobby; public function __construct($name, $hobby){ $this->name = $name; $this->hobby = $hobby; } //输出该对象的信息 public function showInfo(){ echo '<br> 名字是 ' . $this->name; foreach($this->hobby as $hobby){ echo '<br> 爱好有 ' . $hobby; } } //会做算术题, 保护的 protected function getSum($num1, $num2){ return $num1 + $num2; } //编写这个__call魔术方法, __call 魔术方法会接收到两个参数 /* @param $method_name 就是函数名 @param $parameters 就是参数,类型是array */ public function __call($method_name, $parameters){ // echo '<br> method_name = ' . $method_name; // echo '<br> $parameters <br>'; // var_dump($parameters); //判断 $this 中是否有 $method_name 函数,有就返回true,否则返回false if(method_exists($this, $method_name)){ return $this->$method_name($parameters[0], $parameters[1]); }else{ return '没有你要调用的函数'; } } } $monk = new Monk('济公', array('no1'=>'腾云驾雾', 'no2'=>'喝酒')); $monk->showInfo(); //当我们直接调用 protected 方法时,就会触发 __call 魔术方法 echo '<br> 结果是' . $monk->getSum(100, 200);
Exercise questions:
<?php header('content-type:text/html;charset=utf-8'); /* 练习题: 请编写一个Cat类(有 年龄, 名字 二个属性),要求二个属性全部都是public。 Cat类有一个 方法 jiSuan($n1, $n2, $oper) 可以计算+ - * / 是私有的. 在类外部,$对象名->play('jiSuan', $n1, $n2, $oper) 得到结果,注意play这个方法,在类中没有定义. 要求 play 是固定的,如果没有按规则写,则给出相应的错误提示! */ class Cat{ public $name; public $age; public function __construct($name, $age){ $this->name = $name; $this->age = $age; } private function jiSuan($n1, $n2, $oper){ $res = 0; switch($oper){ case '+': $res = $n1 + $n2; break; case '-': $res = $n1 - $n2; break; case '*': $res = $n1 * $n2; break; case '/': $res = $n1 / $n2; break; default : echo '你输入的运算符号不对'; } return $res; } //编写一个__call 魔术方法 public function __call($method_name, $parameters){ //先判断是否通过 'play' 调用 if($method_name == 'play'){ //继续 if( method_exists($this, $parameters[0]) ){ //继续 return $this->$parameters[0]($parameters[1], $parameters[2], $parameters[3]); }else{ return ' 你调用的 ' . $parameters[0] . ' 不存在'; } }else{ return ' 你调用的方式有问题 '; } } } $cat = new Cat('小花猫', 3); echo '<br> 运算的结果是 ' . $cat->play('jiSuan', 10, 20, '-');
The above is the detailed content of How to use the object-oriented magic method __call function in PHP. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

SublimeText3 Chinese version
Chinese version, very easy to use

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

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.