Zend PHP5 认证考试研究之7:PHP4 和 PHP5

由于很多网站主机提供商以及活跃的网站仍然使用PHP4,并且这一情况好像会保持一段时间,PHP5的认证考试有一个关于PHP4和PHP5之间之不同的主题。你必须对PHP5中对于哪个主题以及旧有的代码有很大的改进有一个很好的理解。在考试中,我确实发了一些对于Language Basic以及Function topics的评论,因为我认为这些主题很“基本”以至于不值得写出来,并且你可能应当快速或已经懂得它们了。然而,本文以一些基本概念开始,然后移到PHP5中相对于老板本来讲的一些不同之处;因为,我认为这个帖子是关于PHP上“语言层面”的主题的。于是,这里带来“语言建构到底是什么”……

PHP language construct

The “exact” definition for “Language Construct”.

It’s kind of like the “core language definition” of things such as: if (…) while (…) <?php and ?> and so on In PHP, a handful of common things that many beginners *THINK* are functions are actually Language Constructs: require include echo isset I belive most of them are documented as such in the manual now. Language Construct roughly corresponds to the “grammar” of the language, if you will.

This means that they are integral part of the language, just like ‘if’, or any operator +, *, etc, though they look pretty much like functions. It means that though syntactically they look a functions and in the manual they are listed under functions, this is only to make them easy to use, understand and locate in the documentation. That is why certain rules for functions can be relaxed, such as requiring parenthesis around its arguments, which are not actually needed but are there so that they have the same look as regular functions.

Language Features
PHP 5 allows limited type hinting. This allows you to specify that the parameter to a function or class method can only be of a specific class (or one of its subclasses), or an array. However, you may not specify any other scalar types.

The foreach construct now supports by-reference declaration of the value element.

A number of new functions, particularly for string and array manipulation, has also been added to the core platform.

OOP
For all intents and purposes, all objects in PHP 5 are passed by reference. This means that assigning an object to a variable will not create a copy of the former, but simply creates another reference to it.

Constants, as well as static methods and properties, can now be defined within the scope of a class.

Class methods and properties now feature visibility, and can be declared as public, private or protected. Classes and methods can also be declared as final to prevent further inheritance.

Since all objects are assigned by reference, you now need a specialized mechanism to copy objects. This is provided by the clone construct and the __clone() magic method.

PHP 5 features unified constructors and destructors—all constructors should now be named __construct(), and the new __destruct() magic method has been added for object destruction.

With the addition of interfaces and abstract classes, PHP developers now have far greater control over how they implement their object-oriented code. Interfaces can be used to define common APIs, while abstract classes provide models for class implementations that follow a specific blueprint.

Class definitions can now be loaded on demand by using the __autoload() function.

Magic Methods
A multitude of new “magic” methods has been introduced in PHP 5:

.__get() and __set() are called when accessing or assigning an undefined object property, while __call() is executed when calling a non-existent method of a class.

.__isset() is called when passing an undefined property to the isset() construct.

.__unset() is called when passing an undefined property to unset().

.__toString() is called when trying to directly echo or print() an object.

.__set_state() is inserted dynamically by var_export() to allow for reinitialization on execution of var_export()’s output.

Selected New Extensions
.SimpleXML allows easy access to XML data using object and array notation.

.PHP 5 also introduces a DOMXML, DOMXSL and Sablotron replacement in the form of the libxml2-based DOM and XSL extensions.

.The PHP Data Objects (PDO) extension provides a unified database access extension that allows access to many different types of database systems by using a common interface. PDO is not an abstraction layer—except for prepared queries, it does nothing to abstract the actual database code (SQL), itself.

.The hash extension is a new replacement for the GPLed libmhash; it was added to the PHP core starting with version 5.1.2. It can produce hashes using many algorithms, including the familiar MD5and SHA1, aswell as some more secure (albeit slower) algorithms, such as snefru.

.The Standard PHP Library (SPL) provides numerous interfaces that enhance the way classes interact with the PHP language, including the new Iterator interfaces.

.The new Reflection extension allows for runtime introspection of executing PHP code.

Error Management
.Classes now support exceptions; the new set_exception_handler() function allows you to define a script-wide exception handler.

.The E_STRICT error reporting level has been added to the language to emit notices when legacy or deprecated code is encountered.

.Not included in E_ALL:
– Non static method calls
– By reference issues
– var key word

Conclusion:

. ByRef rather than copying
. $myClone = clone $original;
. __clone() function called on clone if it exists
. Constructor and Destructor
. Magic Methods (in OO Section)
. Class constants
. SimpleXML
. PDO
. SPL
. instanceOf
. Type hinting

Objects got better, faster
The way we handled objects got better
E_STRICT is available, it’s not included in E_ALL



感谢您阅读本文章。您现在可以 Read Comments (14)

日志信息

本日志发布于   1月 16, 2008   8:01 下午   发布在   php   标记为:,,,

你可以跟进任何对此文章的回复通过 Comments Feed. 你可以 留一个评论 ,或 一个 Trackback .



前一日志 Zend PHP5 认证考试研究之6:安全 »
下一日志 Zend PHP5 认证考试研究之8:PHP5 中的 OOP »

阅读更多

相关阅读:

14 回复 到 “ Zend PHP5 认证考试研究之7:PHP4 和 PHP5

  • 1
    Heena
    5月 29, 2008 6:51 下午

    hi,

    i got lots of information for zend certification. its very helpful… i have one question if can help me.

    consider the following code block.
    <?
    function &myfun(){
    $str = “mystr”;
    var_dump($str);
    return ($undefined);
    }
    echo “”;
    for($i=0;$i none of the above
    2> this could would cause an automatic segmentation fault in php4
    3> this code would throw a syntax error in php4
    4> returning an undefined variable by reference in php4 would cause eventual memory corruption
    5> you could not return undefined variable by reference in php4

    what could be the answer?

    please help
    thanks

  • 2
    Heena
    5月 29, 2008 6:52 下午

    <?
    function &myfun(){
    $str = “mystr”;
    var_dump($str);
    return ($undefined);
    }
    echo “”;
    for($i=0;$i

  • 3
    Heena
    5月 29, 2008 6:53 下午

    for($i=0;$i

  • 4
    Heena
    5月 29, 2008 6:54 下午

    hey i can not post the code?

  • 5
    Heena
    5月 29, 2008 6:54 下午

  • 6
    Heena
    5月 29, 2008 6:55 下午

    less than 10;$i++){
    $retval = myfun();
    }
    ?>

  • 7
    admin
    5月 29, 2008 9:36 下午

    Hi,
    First, I am sorry if the for($i=0; $i<10;$i++)…

  • 8
    admin
    5月 29, 2008 9:53 下午

    had the problem. But, to me it seems fine.

    As to the question, I think the answer is 4).

    This code in PHP5 is definitely cause an error. But, in PHP4, there seems some problem found on “return by reference” in the language itself.

  • 9
    Heena
    5月 31, 2008 10:34 上午

    Consider the following php4 code:

    what if any potential compatibility problems will this conditional have in php5?
    1>this code is undefined in php4
    2>none of above
    3>$obj1 and $obj2 musr have the same property values in php5
    4>$obj1 and $obj2 must be the same instance in php5
    5>there are no compatibility issues
    please answer
    thanks

  • 10
    Heena
    5月 31, 2008 10:36 上午

    hey there might be some problem i cant post php code

  • 11
    admin
    5月 31, 2008 3:10 下午

    Do you mean post the code containing the php tags: < ?php and ?>?

  • 12
    Heena
    6月 1, 2008 8:04 上午

    yes I tried to post with tag… btw code is here
    if($obj1===$obj2){
    //do something
    }

  • 13
    admin
    6月 1, 2008 2:17 下午

    The answer is 4>$obj1 and $obj2 must be the same instance in php5.

  • 14
    mcloide
    8月 26, 2008 8:01 上午

    Hi Heena, about the first question, the correct answer would be 3. PHP4 will throw a notice error for using an undefined variable.



留一个评论

注意:任何可以被发表的评论仅仅由于本站所有者同意这样做,任何评论都可能由本站所有者处于任何理由而删除。