PHP » PYTHON |
login |
register |
about
|
PYTHON Constructors and Destructors
is this article helpful?
|
Python replacement for PHP's Constructors and Destructors
[
edit
| history
]
def __init__(self, args...): #constructor pass def __del__(self): #destructor pass PHP Constructors and DestructorsPHP original manual for Constructors and Destructors [ show | php.net ]Constructors and DestructorsConstructorPHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Example #1 using new unified constructors
<?phpFor backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics. Destructor
void __destruct
( void
)
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence. Example #2 Destructor Example
<?phpLike constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent::__destruct() in the destructor body.
|
more
Recently updated
more
Most requested
|