Imeetsia takoi kod :
/* Testirovanie objektnoi modeli v PHP 5.0.0 beta 1 */
class testing_PHP5_version
{
private $x; // zakritaja peremennaja
function __construct() // Konstruktor klassa
{
echo "<br><br><b>Constructor of testing_PHP5_version class started...</b> <br><br>";
}
public function public_function() // otkritaja funkcija
{
print "I am <b>public</b> function!!!<br>";
}
protected function protected_function() // zakritaja funkcija dostupnaja tolko klassu i naslednikam
{
$this->private_function();
print "I am <b>protected</b> function!!!<br>";
}
private function private_function() // zakritaja funkcija dostupnaja tolko dannomu klassu
{
$this->x = 3;
print "I am <b>private</b> function!!!<br>";
}
function __destruct() // Destruktor klassa
{
print "<br><br><b>Destructor of testing_PHP5_version class started...</b> <br><br>";
}
}
class testing_PHP5_version2 extends testing_PHP5_version // naslednik klass testing_PHP5_version
{
function __construct()
{
echo "<br><br><b>Constructor of testing_PHP5_version2 class started...</b> <br><br>";
}
public function display() // otkritaja funkcija
{
$this->protected_function();
$this->public_function();
/* Popitka vizova zakritogo metoda */
//$this->private_function(); // Ne dolzhno rabotat
}
function __destruct()
{
print "<br><br><b>Destructor of testing_PHP5_version2 class started...</b> <br><br>";
}
}
$x = new testing_PHP5_version;
$x->public_function();
$x2 = new testing_PHP5_version2;
$x2->display();
/***********************************************************/
class Counter
{
var $counter = 0;
public function increment_and_print()
{
print ++$this->counter;
print "\n";
}
}
class SingletonCounter
{
static $m_instance = NULL;
function Instance()
{
if (self::$m_instance == NULL)
{
self::$m_instance = new Counter();
}
return self::$m_instance;
}
}
for ($i = 0; $i < 15; $i++)
{
SingletonCounter::Instance()->increment_and_print();
}
/************************************************************/
Rezultat takoi :
Constructor of testing_PHP5_version class started...
I am public function!!!
Constructor of testing_PHP5_version2 class started...
I am private function!!!
I am protected function!!!
I am public function!!!
//Vot etot moment neponiaten, pochemu snachala otrbativajut poslednie dva klassa i tolko potom rabotajut destruktori
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Rezultat raboti poslednih dvuh klassov
Destructor of testing_PHP5_version class started...
Destructor of testing_PHP5_version2 class started...