ru en uk

  sing in

(044) 362 48 16   (098) 294 41 60


   Services
   Portfolio
   Prices
   Articles
   Services
   Printing
   Multimedia
   Hosting
   Contacts

Home   |   WEB development   |   Articles   |   Programming in PHP

PHP5 is not officially released yet, but "working" version is already for work (as well as unstable!), So that we can begin to explore new opportunities for the next release of PHP and to practice with them. In this article we will talk about the three major innovations in PHP5:

top: 5px; margin-left: 60px ">
  • New object model
  • Exceptions
  • Namespaces
  • But the first couple of official statements:

    • Some of the solutions in this article are reproducible in PHP4, but, nevertheless,description of them is present here for more readability and integrity of the entire article.
    • Some are described in this article features in the final release of PHP5 can be changed.

    PHP5 is not yet released, and I do not know when this will happen, but now you can around without and explore new possibilities of language, download a working version of PHP5 with http://snaps.php.net and install it. This link can be found ready to install Windows and Linux versions of PHP5. Installation goes like a normal release of PHP, sothat all the running for a new toy.

    New object model

    In the PHP5 object model thoroughly podlatali and added many new features, so that PHP5 has become "something" like Java. In this part of our article will describe this newI object model and are a few small examples to you to identify the source point for your experiments.

    • Constructors and Destructors
    • Objects as links
    • Cloning objects
    • Descriptors Private, Publicand Protected
    • Interfaces
    • Abstract Classes
    • __call
    • __set and __get
    • Private Member
    Constructors and Destructors

    In PHP4 constructor is called as well as the class itself, but there are no destructors in full. In PHP5 class constructor is called __construct, and destructor - __destruct.

    Example 1: Constructors and Destructors

    <? php
    clas
    s foo (
    var
    $ x;

    function
    __constructlor: # 007700 "> ($ x) (
    $ this -> x = $ x;
    )

    function
    display () (
    print ($
    this an> -> x);
    )

    function __destruct () (
    print (<
    / span> "Well, so far, and soon");
    )
    )

    $ o1 = new foo pan> (4);
    $ o1 -> display ();
    yle = "color: # 0000bb">?>

    As you can see, the destructor is called just before the destruction of the class.

    Objects as links

    As you probably already know, in PHP4 variables passed to functions / methods by value (a copy is transmitted), If the function declaration does not put the symbol '&', indicating that the variable should be passed as a reference. In PHP5 objects are always passed as references. Assignment of the same happening here.

    Example 2: Objects as links

    ass = "highlight"> <? php
    class foo (
    var
    $ x
    le = "color: # 007700">;

    function setX ($ x) (
    $ this x = $ x;
    )

    function
    getX < span style = "color: # 007700"> () (
    return $ this -> x;
    )
    )

    or: # 0000bb "> $ o1 = new foo;
    $ o1 -> setX or: # 007700 "> (4);
    $ o2 = $ o1;
    b "> $ o1 -> setX (5);
    if ($
    o1 -> "color: # 0000bb"> getX () == $ o2 -> getX ()) print ( "Oh & nbsp; thou, O my God! ");
    >

    Cloning objects

    If the objects are assigned and transmitted on the link, you will need to once and create a copy of bektov. Use the method __clone.

    Example 3: Cloning of

    <? php
    class
    "> foo (
    var
    $ x;

    function
    setX ( b "> $ x) (
    $ this -> x = $ xtyle = "color: # 007700">;
    )

    function getX () (
    return
    $ this -> < span style = "color: # 0000bb"> x;
    )
    )

    $ o1 = new foo;
    style = "color: # 0000bb"> $ o1 -> setX (4);
    $ o207700 "> = $ o1 -> __clone ();
    $ o1 -> setX (5);

    if ($
    o1 -> getX ()! = & nbsp; $ o2 -> getX ()) print ( "Copies vzaimonezavisimy");
    >>

    In programming cloning is permitted, so that all legal ;-)

    Descriptors Private, Public and Protected

    In PHP4, all methods and variables within the facility are accessible from the outside, in other wordss all methods and variables have always been open. In PHP5 introduces three descriptor to control access to variables and methods: Public, Protected and Private.

    • Public (open): The method / variable can be accessed from anywhere in the code. < br />
    • Private (private): The private methods or variables are only available within the class.
    • Protected (protected): protected methods or variables are only available within the class where they were declared, and its derivative classes.

    d "> Example 4: Public, protected and private

    <? php
    class foo (
    private
    ; $ X;

    public function
    public_foo () (
    print (
    "This open method ");
    )

    protected function
    protected_foo () (
    bb "> $ this -> private_foo (); / / All right, we can call private methods, because we findmsya in the same class
    print ( "This is a secure method");
    )

    private function
    private_foo () (
    $ this -> x = pan style = "color: # 0000bb"> 3;
    print (
    "This is a private method");
    )
    )

    class
    bb "> foo2 extends foo (
    public function
    display () (
    & nb
    sp; $ this -> protected_foo ();
    $ this -> < span style = "color: # 0000bb"> public_foo ();
    / / $ This-> private_foo (); / / Wrong! The basic method of the class is closed
    = "color: # 007700">)
    )

    $ x = new foo ();
    $ x ->public_foo ();
    / / $ x-> protected_foo (); / / False protected methods can be invoked only from the same class, or eOn the derivatives of classes
    / / $ x-> private_foo (); / / Wrong, private methods can be called in only in the classroom, where they were declared

    $ x2 </ span> = new foo2 ();
    $ x2 -> display ();
    >

    Tip developers: Variables of class should always be done in private, direct access to the variable - not a good practice in the PLO, it is best to access / change the class variables to determine the specific methods.

    As you know, PHP4 supports inheritance class syntax "class foo extends parent". In PHP4 And PHP5 class can only inherit one class, that is, multiple inheritance is not supported. Interface is called the classwhich is not realized any single method, identifies only the name and a set of parameters referred to them. Subsequently, classes can 'sell' any number of interfaces, thereby showing that a class implements the methods defined in the interface.

    <? php
    interface displayable (
    f
    unction display ();
    )

    interface
    printable (
    function
    doprint ();
    )

    class foo implements displayable, printable (
    function display () (
    / / Code
    )
    /> Function doprint () (
    / / Code
    )
    )
    ">>

    Using interfaces is useful for easy reading and understanding the code: Read the announcement of class, we can see that the class implements the interface displayable and printable; this means that the class must have methods of display () and doprint (). As these methods are implementeds - does not matter, the most important - from the class definition, you know that you can call these methods.

    Abstract Classes

    Is called an abstract class that can only be used as a base (that is to create objects of that class should not be). Kato normal in any base class, in the abstract class you can define methods and variables.

    In the abstract class, you can also define abstract methods: methods that are not implemented in the abstract class, but that must be implemented in derived classes. />

    Example 6: Abstract Classes

    <? php
    abstract class foo
    # 007700 "> (
    protected $ x;

    abstract function
    display ();

    function
    setX ($ x) (
    $ this -> > x = $ x;
    )
    )


    class
    foo2 extends olor: # 0000bb "> foo (
    function
    display () (
    / / Code
    olor: # 007700 ">)
    )
    >

    __call

    With PHP5, you can implement in the class a special method __call (), as a method to "capture" all of the outstanding in this class of methods. Method__call (if defined) is called when trying to invoke a method unavailable or nonexistent.

    Example 7: __call

    <? php
    class & n
    bsp; foo (

    function
    __call ($ name, = "color: # 0000bb"> $ arguments) (
    print (
    "cause? I - $ name!");
    )
    )

    or: # 0000bb "> $ x = new foo ();
    $ x -> doStuffcolor: # 007700 ">();
    $ x -> fancy_stuff ();
    >

    This ad hoc method can beused to implement overload methods: you can explore from the arguments and depending on the result of a suitable cause for this event closed method, for example:

    Example 8: Overloading methods using __call

    <? php
    class Magic (

    function
    __call
    yle = "color: # 007700"> ($ name, $ arguments) (
    if ($
    name> == 'Foo') (
    if (is_int ($
    arguments [ 0])) $ this -> foo_for_int ($ arguments an style = "color: # 007700"> [0]);
    if (is_string ($
    arguments or: # 007700 "> [0])) $ this -> foo_for_string ( > $ arguments [0]);
    )
    )

    private function
    foo_for_int: # 007700 "> ($ x) (
    print (
    "a, look, a whole number!");
    )

    & nbs
    p; private function foo_for_string ($ x) (
    print (
    "a, look, string! ");
    )
    )

    $ x = new Magic ();
    : # 0000bb "> $ x -> foo (3);
    $ x -> "color: # 0000bb"> foo ( "3");
    >

    __set and __get

    But this is a e all, you can now define the methods and __get __set to "capture" all attempts to change or access to an uncertain (or inaccessible) variables.

    Example 9: __set and __get

    r: # 0000bb "> <? php
    class foo (

    function
    __set (# 0000bb "> $ name, $ val) (
    print (
    "Hi, you tried to assign the value $ val variable$ name ");
    )

    function
    __get ($ name) (
    & nbs
    p; print ( "Hi, you tried to access $ name");
    )
    )

    $ x = newfoo ();
    $ x -> bar = 3 n style = "color: # 007700">;
    print ($ x -> winky_winky);
    >

    small_caption "> Indication of types of arguments

    In PHP5, you can "tell" method, that he should get in an argument to the object type.

    Example 10: an indication of the types

    tyle = "color: # 0000bb"> <? php
    class foo (
    / / Code ...
    )

    cl
    ass bar (
    public function
    process_a_foo (foo $ foo 07700 ">) (
    / / Even some code
    )
    )

    $ b = newstyle = "color: # 0000bb"> bar ();
    $ f = new foo ();
    $ b process_a_foo ($ f);
    >

    As you notice, the nameargument, you can now put the name of his class, and thus PHP5 determines that the variable $ foo to be of class foo.

    Static class members

    Static members and static methods can be used for the realization of the fact that the PLO nazyvaetsI "class methods and class variables."

    The static method of class "call a method that can be called without creating an object of this class.
    "Variable-class" is called a variable to which you can apply without creating an object of this class (and access method is not required).

    Example 11: Class methods and class variables

    <? php
    class
    : # 0000bb "> calculator (
    static public
    $ pi = 3.14151692;

    & nb
    sp; static public function add ($ x, $ y) (
    &
    nbsp; return $ x + $ y;
    )
    )

    $ s = Calculator:: $ pi;
    $ result ="> calculator:: add (3, 7);
    print (
    result");
    >

    Exceptions

    Exceptions - This universal approach to handling errors and unexpected situations in the German ykah as Java and C + +; in PHP5 intercepting exceptions implemented using a pair of "try" - "catch".

    Example 12: Exceptions

    <? php
    : # 007700 "> class foo (

    function
    divide ($ x, $ y) (
    if ($
    y == 0) throw new n style = "color: # 0000bb"> Exception ( "division by zero is inadmissible");
    return
    $ x color: # 007700 "> / $ y;
    )
    )

    $ x = new foo 00 ">();

    try (
    $ x -> divide (3, "color: # 0000bb"> 0);
    ) Catch (Exception
    $ e) (
    echo
    $ e r: # 007700 "> -> getMessage ();
    echo
    " n <br /> n";
    color: # ff8000 "> / / Are there any draconian measures
    )
    >

    As you can see, "try" is used to denote the block in which errors are processed by operatorsOromo "catch", which is at the end of the block. In the block "catch" you need to implement your own policies for handling errors. As a result, we obtain a legible code, and only one block of error handling.

    The exceptions, user-defined To deal with unforeseen problems in your programs, you can define your own exceptions. All you have to - it's easy to supplement (extend) a class Exception, setting the class constructor and the method getMessage.

    Example 13: Exceptions, ODA edelennye user

    <? php
    class WeirdProblem extends
    0000bb "> Exception (

    private
    $ data;

    function
    WeirdProblem : # 007700 "> ($ data) (
    parent::
    exception or: # 007700 ">();
    $ this -> data = $ datae = "color: # 007700">;
    )

    function getMessage () (
    return
    $ this -> data. "aroused some strange exception!"
    )
    )
    >

    Then, for the initiation of certain exceptions to use the structure you throw new WeirdProblem ($ foo); if an exception occurs inside a block try (), it will transfer management of PHP5 to "catch"-block lengthI treatment.

    Namespaces

    For convenience, classes and functions can be grouped into namespaces (namespaces).

    Note: The developers refused to poddrezhki this possibility.

    Sample 14: Namespace

    <? php
    namespace Math
    (

    class
    Complex
    "> (
    //... code ...
    function __construct () (
    pri
    nt ( "hello");
    )
    )
    )

    $ m = newMath:: Complex ();
    >

    Pay attention to the syntax of the use of personal space to indicate the class, subject totorogo we create. An example of practical application: the creation of corresponding classes in different names spaces, while the classes do different jobs (with the same interface).


     
    Working with databases
    29.05.2007
    Working with databases
    29.05.2007
    Working with databases
    29.05.2007