Home Backend Development PHP Tutorial 【转】PHP5.3亲测精髓特性整理

【转】PHP5.3亲测精髓特性整理

Jun 13, 2016 pm 01:07 PM
class echo function namespace static

【转】PHP5.3亲测精华特性整理
1、引入 __DIR__ magic常量 定位脚本的目录

    echo __DIR__."|".dirname(__FILE__);
Copy after login


2、允许从2个值的or/and表达式快速的获取一个非空的值

    echo (1 ?: 0) ."|". ("" ?: 0)."|". var_export(array() ?: array(1), true);
Copy after login


3、类添加__callStatic方法,等价于 __call(),但它是为调用静态方法准备的。下面来看示例:

    class xxx{ 
        function __call($func, $args){ 
            echo "function name:".$func; 
            echo "</br />"; 
            echo "args:".var_export($args, true); 
        } 
        static function __callStatic($func, $args){ 
            echo "static function name:".$func; 
            echo "</br />"; 
            echo "static args:".var_export($args, true); 
        } 
    } 
    $x = new xxx(); 
    $x->testfunc("oo"); 
    echo "</br />"; 
    xxx::teststatic("xx");
Copy after login


4、动态的调用静态的方法

 
   class xx{ 
        static function test(){ 
            echo __METHOD__; 
        } 
    } 
    $c = "xx"; 
    $m = "test"; 
    $c::$m();
Copy after login


5、延迟静态绑定(静态处理从编译时延迟到执行时)

class A{ 
    static function whoami(){ 
        echo __CLASS__; 
    } 
 
    static function say1(){ 
        self::whoami(); 
    } 
 
    static function say2(){ 
        static::whoami(); 
    } 
} 
 
class B extends A{ 
    static function whoami(){ 
        echo __CLASS__; 
    } 
} 
 
class C extends A{ 
    static function whoami(){ 
        echo __CLASS__; 
    } 
} 
 
B::say1(); 
C::say2();
Copy after login


6、MySQLInd (高速的专门为PHP设计的MySQL调用库接口)
7、INI Magic
8、扩展的 OpenSSL 函数
9、SPL(Standard PHP Library) 优化
10、时间处理进行扩展了和添加

    
    $date = strtotime("11-05-17 00:00:00"); 
    echo date("Y-m-d", $date)."|"; 
 
    $date = date_create_from_format("y-m-d", "11-05-17"); 
    echo $date->format("Y-m-d");
Copy after login


11、垃圾回收器(为复杂和长时间运行脚本的执行结束周期释放内存的清理)

     
   gc_enable(); // 允许垃圾回收 
    var_dump(gc_enabled()); // true 
 
    var_dump(gc_collect_cycles()); // 某个元素的清理 
 
    gc_disable(); // 禁止垃圾回收
Copy after login


12、NOWDOC(不对HEREDOC进行转译)

    $fubar = "xxx"; 
 
    $xbar = <<<ONE 
        this is HEREDOC $fubar 
    ONE; 
 
    $abar = <<<'TWO' 
        this is NOWDOC $fubar 
    TWO; 
    echo $xbar."|".$abar;
Copy after login


13、匿名函数体验

    $ofun = create_function('$a, $b', 'return $a*$b;'); //不要忘了分号,传统方法 
    echo $ofun(2, 5); 
 
    $nfun = function ($a, $b){ 
        return $a*$b; 
    };//不要忘了分号,现代方法 
 
    echo $nfun(3, 5);
Copy after login


14、闭包体验

    $arr = array(1, 2, 3, 5, 6, 7); 
    $res = array_filter($arr, function($v) { return $v >4; }); 
    var_dump($res); 
 
    function countnum(){ 
        $num = 0; 
        $testt = function() use(&$num){ 
            ++$num; 
            echo $num; 
        }; 
        return $testt; 
    } 
 
    $t = countnum(); 
    $t(); 
    $t();
Copy after login


15、命名空间

    namespace xxx; //第一个namespace需要在所有输出之前 
    function t(){ 
        return __NAMESPACE__; 
    } 
 
    namespace xxx; 
 
    function t(){ 
        return __NAMESPACE__; 
    } 
 
    echo \xx\t()."|"; 
    echo \xxx\t();
Copy after login

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)

How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Aug 26, 2023 pm 10:58 PM

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? In Vue development, we often use the v-bind instruction to dynamically bind class and style, but sometimes we may encounter some problems, such as being unable to correctly use v-bind to bind class and style. In this article, I will explain the cause of this problem and provide you with a solution. First, let’s understand the v-bind directive. v-bind is used to bind V

See all articles