PHP

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • __construct __destruct in PHP 4

    3 answers - 615 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi,
    I have been trying going thru the PHP manual to find if there are any
    equivalent to the __contruct and __destruct in PHP 4, but I cannot find any
    solution for this part. I know it was introduced in PHP 5, but as __sleep
    and __wakeup exist in PHP 4 already I was hoping there is something like
    __init and __die in PHP 4 :-)
    Best regards,
    Peter Lauri
    <http://www.dwsasia.com/www.dwsasia.com - company web site
    <http://www.lauri.se/www.lauri.se - personal web site
    <http://www.carbonfree.org.uk/www.carbonfree.org.uk - become Carbon Free
  • No.1 | | 612 bytes | |

    I have been trying going thru the PHP manual to find if there are any
    equivalent to the __contruct and __destruct in PHP 4, but I cannot
    find any
    solution for this part. I know it was introduced in PHP 5, but as
    __sleep
    and __wakeup exist in PHP 4 already I was hoping there is something
    like
    __init and __die in PHP 4 :-)

    In PHP 4 the constructor has the same name as the class (like C++). See

    There is no destructor in PHP 4.

    Larry

    PS The manual has two sets of P documentation: one for PHP 4 &
    another for PHP 5. Make sure you're viewing the right set.
  • No.2 | | 836 bytes | |

    Sun, January 28, 2007 6:56 am, Peter Lauri wrote:
    I have been trying going thru the PHP manual to find if there are any
    equivalent to the __contruct and __destruct in PHP 4, but I cannot
    find any
    solution for this part. I know it was introduced in PHP 5, but as
    __sleep
    and __wakeup exist in PHP 4 already I was hoping there is something
    like
    __init and __die in PHP 4 :-)

    Read the ChangeLog, but as I recall, they were introducted in PHP 5
    and simply had no corresponding functionality in PHP 4

    Though you might be able to hack something with __sleep and __wakeup
    in some way to detect death and construction.

    Actually, construction should not be terribly difficult, as the parent
    constructor is always called, so you can do whatever you want in that
    instead of in __construct.
  • No.3 | | 1289 bytes | |

    Peter Lauri wrote:
    Hi,

    I have been trying going thru the PHP manual to find if there are any
    equivalent to the __contruct and __destruct in PHP 4, but I cannot find any
    solution for this part. I know it was introduced in PHP 5, but as __sleep
    and __wakeup exist in PHP 4 already I was hoping there is something like
    __init and __die in PHP 4 :-)

    Hi Peter,

    As you probably already know, PHP 4 constructors are functions named
    after the class

    <?php
    class Foo
    {
    function Foo()
    {
    echo "in constructor";
    }
    }
    $a = new Foo;
    ?>

    Although destructor emulation is implemented in the PEAR class
    (destructor would be function _Foo), I don't recommend using a
    destructor in PHP 4. Instead, manually destruct your objects. PEAR
    does it with a shutdown function, so if performance is not a question,
    by all means, use PEAR's implementation. It has been battle-tested for
    years and years.

    However, I wouldn't bother with writing new code in PHP 4. It will be
    cheaper for you to switch hosting providers to one that allows you to
    use PHP 5. Why? You will waste tons of time working around reference
    issues that are non-existent in PHP 5 due to object handles.

    Greg

Re: __construct __destruct in PHP 4


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 1 =
QUESTION ON "PHP"

EMSDN.COM