PHP object is a few similarities and differences with java,c++,
1.PHP classes can duplicate constructor __construct and destructors, which are similar to C + +, and default to parameterless constructs and destructors if not written
<?php
class Nbaplayer {
}
$jordan = new Nbaplayer ();
Replication construction and destructor functions
<?php
class Nbaplayer {public
$name;
public $height;
public $weight;
public $team;
public $teamNumber;
function __construct ($name, $height, $weight, $team, $teamNumber) {
$this->name = $name;
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
$this->teamnumber = $teamNumber;
echo "Construc A instance:". $name. " <br/> ";
}
function __destruct () {
echo "Destroy A instance:". $this->name. " <br/> ";
}
}
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "n");
$james = new Nbaplayer ("James", "205cm", "120kg", "Heat", "10");
2. $jordan is a reference to an object, and the memory release of the PHP object takes a reference count (similar to Java), which is automatically invoked by the GC at 0.
& Create a Shadow (alias) reference without increasing the reference count.
<?php
class Nbaplayer {public
$name;
public $height;
public $weight;
public $team;
public $teamNumber;
function __construct ($name, $height, $weight, $team, $teamNumber) {
$this->name = $name;
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
$this->teamnumber = $teamNumber;
echo "Construc A instance:". $name. " <br/> ";
}
function __destruct () {
echo "Destroy A instance:". $this->name. " <br/> ";
}
}
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "n");
$james = new Nbaplayer ("James", "205cm", "120kg", "Heat", "ten");
$james 1 = $james//is equivalent to creating a new reference for the James object
$james 2 = & $james 1;//Create a shadow reference, which is actually an alias
$james = null;
echo "James1 has not yet set to be nulled <br/>";
$james 1 = null;//james object Invocation destructor (whose reference count is 0)
echo "James1 has set to being nulled<br/>";
? >
Run Result:
Construc a Instance:jordan
construc a instance:james
james1 has not yet set to is nulled
Destroy a Instance:ja Mes
James1 has set to be nulled
Destroy a Instance:jordan