- BASICS
- Quotes
- Constants
- Control Structures
- Reference
- Number Systems
- VARIABLES
- Definition
- Variable Variable
- Exists
- Type Casting
- OPERATORS
- Aritmetic
- Bitwise
- String
- Comparison
- Logical
- FUNCTION
- Definition
- Anonymous
- Reference
- Variable Arguments
- ARRAY
- Basics
- Operations
- Create
- Search
- Modify
- Sort
- Storage
- STRING
- String Basics
- String Compare
- String Search
- String Replace
- String Format
- String Regexp
- String Parse
- Formating
- Json
- STREAMS
- File Open
- Read File
- Read Csv
- File Contents
- Context
- Ob_start
- OOP
- Object Instantiation
-
Class Constructor
- Interfaces, Abstract
- Resource Visibility
- Class Constants
- Namespaces
- HTTP
- Headers
- File Uploads
- Cookies
- Sessions
Contruct
Php uses the magic __construct() method as constructor. Constructor is usefull for initializing object's properties.
class A
{
function __construct()
{
echo __METHOD__;
}
}
$obj = new A(); // A::__construct
Destruct
You can unset() or overwrite a variable that is reference for an object. The object itself may not be destroyed. The reference to it is held elsewhere.
class A
{
function __construct() {}
function foo()
{
echo "A is alive";
}
}
$a = new A();
echo $a->foo(); // A is alive
$b = $a;
unset($a);
echo $a->foo(); // Fatal error: ... non-object
echo $b->foo(); // A is alive
The __destruct() method is called at the end of script execution.
class A
{
function __construct()
{
echo "A is alive";
}
function __destruct()
{
echo "A is dead";
}
}
$a = new A(); // A is alive / A is dead
Last update: 525 days ago