in Deutsh
in French
in Polish

You Are At Area


recent searches:
include functions , variable functions , post functions...




If you are new to PHP or just need to refresh your skills, this is the place to start. This series of tutorials will give you the basic knowledge you will need to create a simple PHP website.

PHP is a reflective programming language originally designed for producing dynamic web pages.[1] PHP is used mainly in server-side scripting, but can be used from a command line interface or in standalone graphical applications. Textual User Interfaces can also be created using ncurses.

PHP Manual
PrevChapter 18. Classes and Objects (PHP 4)Next

Constructors

Constructors are functions in a class that are automatically called when you create a new instance of a class with new. A function becomes a constructor, when it has the same name as the class. If a class has no constructor, the constructor of the base class will be called, if it exists.

<?php
class Auto_Cart extends Cart {
    function
Auto_Cart() {
        
$this->add_item("10", 1);
    }
}
?>

This defines a class Auto_Cart that is a Cart plus a constructor which initializes the cart with one item of article number "10" each time a new Auto_Cart is being made with "new". Constructors can take arguments and these arguments can be optional, which makes them much more useful. To be able to still use the class without parameters, all parameters to constructors should be made optional by providing default values.

<?php
class Constructor_Cart extends Cart {
    function
Constructor_Cart($item = "10", $num = 1) {
        
$this->add_item ($item, $num);
    }
}

// Shop the same old boring stuff.
$default_cart = new Constructor_Cart;

// Shop for real...
$different_cart = new Constructor_Cart("20", 17);
?>

You also can use the @ operator to mute errors occurring in the constructor, e.g. @new.

<?php
class A
{
    function
A()
    {
        echo
"I am the constructor of A.<br />\n";
    }

    function
B()
    {
        echo
"I am a regular function named B in class A.<br />\n";
        echo
"I am not a constructor in A.<br />\n";
    }
}

class
B extends A
{
}

// This will call B() as a constructor
$b = new B;
?>

The function B() in class A will suddenly become a constructor in class B, although it was never intended to be. PHP 4 does not care if the function is being defined in class B, or if it has been inherited.

Caution

PHP 4 doesn't call constructors of the base class automatically from a constructor of a derived class. It is your responsibility to propagate the call to constructors upstream where appropriate.

Destructors are functions that are called automatically when an object is destroyed, either with unset() or by simply going out of scope. There are no destructors in PHP. You may use register_shutdown_function() instead to simulate most effects of destructors.

Najlepsza ochrona mienia na rynku
tłumaczenia
opakowania z nadrukiem
Najlepsza organizacja eventów Gorąco Polecamy!
Strony internetowe

Another Useful functions:


migration5.oop | language.oop5.visibility | language.oop5.typehinting | language.oop5.static | language.oop5.reflection | language.oop5.patterns | language.oop5.paamayim-nekudotayim | language.oop5.overloading | language.oop5.object-comparison | language.oop5.magic | language.oop5.iterations | language.oop5.interfaces | language.oop5 | language.oop5.final | language.oop5.decon | language.oop5.constants | language.oop5.cloning | language.oop5.basic | language.oop5.autoload | language.oop5.abstract | language.oop.serialization | language.oop.object-comparison | language.oop.newref | language.oop.magic-functions | language.oop | language.oop.constructor | function.swfsoundinstance.loopoutpoint | function.swfsoundinstance.loopinpoint | function.swfsoundinstance.loopcount |


PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual, and the example archive sites and some of the other resources available in the links section.