1.函数并立即调用
php call_user_func(function(){ echo "hello,world"; });
2.上下文变量
php $context="hello,world"; call_user_func(function()use($context){ echo $context; });
3.use和$this
php class A { public $t; public function __construct($t="hello,world"){ $this->t=$t; } function test(){ call_user_func(function(){ echo $this->t;//php5.4+ }); } } $a=new A(); $a->test();class Hello { private $message = "Hello world\n"; public function createClosure() { return function() { echo $this->message; }; }}class Bye { private $message = "Bye world\n";}$hello = new Hello();$helloPrinter = $hello->createClosure();$helloPrinter(); // outputs "Hello world"$bye = new Bye();$byePrinter = $helloPrinter->bindTo($bye, $bye);//like javascript apply$byePrinter(); // outputs "Bye world"$CI = $this;$callback = function () use (&$CI) {$CI->public_method();};
4.调用一切可调用的东西
php class Contrller{ //调用具体的action, public function __act($action){ call_user_func( array($this,$action) ); //$this->{$action}() }}class HelloContrller extends Controller{ public function index(){ } public function hello(){ } public function dance(){ }}
5.装饰器
php //装饰器 $dec=function($func) { $wrap=function ()use ($func) { echo "before calling you do sth\r\n"; $func(); echo "after calling you can do sth too\r\n "; }; return $wrap; }; //执行某功能的函数 $hello=function (){ echo "hello\r\n"; }; //装饰 $hello=$dec($hello); //在其他地方调用经过装饰的原函数 $hello(); /*output:before calling you do sthhelloafter calling you can do sth too*/
6.object
php$obj = new stdClass;$obj->a = 1;$obj->b = 2;
7.static
php<br />class father{ public function __construct() { // $this->init(); static::init(); } private function init() { echo "father\n"; }}class son extends father{ /*public function __construct() { $this->init(); }*/ public function init() { echo "son\n"; }}$son = new son();son::init();//son
8.list explode
php list( , $mid) = explode(';', 'aa;bb;cc'); echo $mid; //输出 bb
9.object array
php $array = json_decode(json_encode($deeply_nested_object), true); //$array = (array) $yourObject; function object_to_array($data){ if (is_array($data) || is_object($data)) { $result = array(); foreach ($data as $key => $value) { $result[$key] = object_to_array($value); } return $result; } return $data;}
10.tree
php function displayTree($array){ $newline = "<br>"; $output = ""; foreach($array as $key => $value) { if (is_array($value) || is_object($value)) { $value = "Array()" . $newline . "(<ul>" . $this->displayTree($value) . "</ul>)" . $newline; } $output .= "[$key] => " . $value . $newline; } return $output; }
11.交换变量
list($a, $b) = array($b, $a);
来自
知乎
http://stackoverflow.com/questions/8391099/using-this-in-anonymous-fun...
http://stackoverflow.com/questions/4345554/convert-php-object-to-assoc...
https://www.quora.com/What-are-some-cool-PHP-tricks

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。

CSP重要因为它能防范XSS攻击和限制资源加载,提升网站安全性。1.CSP是HTTP响应头的一部分,通过严格策略限制恶意行为。2.基本用法是只允许从同源加载资源。3.高级用法可设置更细粒度的策略,如允许特定域名加载脚本和样式。4.使用Content-Security-Policy-Report-Only头部可调试和优化CSP策略。

HTTP请求方法包括GET、POST、PUT和DELETE,分别用于获取、提交、更新和删除资源。1.GET方法用于获取资源,适用于读取操作。2.POST方法用于提交数据,常用于创建新资源。3.PUT方法用于更新资源,适用于完整更新。4.DELETE方法用于删除资源,适用于删除操作。

HTTPS是一种在HTTP基础上增加安全层的协议,主要通过加密数据保护用户隐私和数据安全。其工作原理包括TLS握手、证书验证和加密通信。实现HTTPS时需注意证书管理、性能影响和混合内容问题。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版
中文版,非常好用

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。