signis Posted March 2, 2006 Report Share Posted March 2, 2006 Zinu, zinu, ni tuda popal, bet izkritis man no praata tas termins, jo gribu atrast piemeeru kaadu php, kaa tas tiek realizeets. Se pat kur forumaa uzduuros kaadu laiku atpakal kaadam rakstam par to un realizeju perlaa, bet gribu tagad ieksh php. Varat aatri izliidzeet? Link to comment Share on other sites More sharing options...
bubu Posted March 2, 2006 Report Share Posted March 2, 2006 Varbūt: creation (create instance of class) class XXX {...} $x = new XXX(); P.S. Lūdzu izvēlies pareizākas foruma sadaļas! Link to comment Share on other sites More sharing options...
signis Posted March 2, 2006 Author Report Share Posted March 2, 2006 atvainojos. nee, nee, tas izkataas apmeeram taa. $class = null; class blablabla { function blablabla { if ($class<>null) {return $class} else { -- tur kautkaadas darbiibas } } } (lūdzu lieto bbtagus. bubu) un ideja ir tur, ka ja tu jau reiz esi to klasi atveeris, tad katrs naakamais new class, tev atgrieziis jau atveerto, ar visiem initzializetiem un uztaisiitiem mainiigajiem un visu stafu. tas vaards bija kautkas ar pattering, vai kautkaa taa. Link to comment Share on other sites More sharing options...
bubu Posted March 2, 2006 Report Share Posted March 2, 2006 Tas ir viens no vienkāršākajiem programming patterniem - saucās kā "Singleton". To parasti taisa šādā veidā: class XXX { function instance() { static $_self = false; if (!$_self) $_self = new XXX(); return $_self; } } $x = XXX::instance(); Link to comment Share on other sites More sharing options...
v3rb0 Posted March 2, 2006 Report Share Posted March 2, 2006 (edited) var arī kaut kādu šitādu atsevišķu klasi class singleton { function &get_instance_of($class) { static $instances = array(); if (!array_key_exists($class, $instances)) $instances[$class] = new $class; return $instances[$class]; } } un tad kur vajag $c1 =& singleton::get_instance_of('class1'); $c2 =& singleton::get_instance_of('class2'); $c3 =& singleton::get_instance_of('class3'); pluss - klasei nav jāliek klāt metode ar kuru dabū singletona instanci. mīnuss - viena lieka klase. Edited March 2, 2006 by v3rb0 Link to comment Share on other sites More sharing options...
signis Posted March 2, 2006 Author Report Share Posted March 2, 2006 cik es saprotu no php oo, tad klasei desas, funkcija desas izpildaas taisot new desas, vai tajaa funkcijaa desas nevar iepihaat singeltone? Link to comment Share on other sites More sharing options...
bubu Posted March 2, 2006 Report Share Posted March 2, 2006 v3rb0: $c1 =& singleton::get_instance_of('class1'); Tātad $class= "class1"; $instances[$class] = new $class; $.. = new "class1" ?? wtf? signis: taisot new klases_vaards vienmēr tiks uztaisīts jauns objekts, tāpēc ar php tā nevar (citās valodās var). P.S. Tai funkcijai ir speciāls nosaukums - konstruktors. Link to comment Share on other sites More sharing options...
v3rb0 Posted March 2, 2006 Report Share Posted March 2, 2006 bubu: ja $class= "class1"; tad $instances['class1'] = new class1; piemērs tam visam http://paste.php.lv/3365 Link to comment Share on other sites More sharing options...
bubu Posted March 2, 2006 Report Share Posted March 2, 2006 Nja, interesanti... Nemaz nezināju ka new operatoram šitādus brīnumus var iebarot (stringu): <?php class A { function x() { return 1; } }; function f($x) { return new $x; } $a = f('A'); echo $a->x(); ?> Mūžu dzīvo, mūžu mācies... Link to comment Share on other sites More sharing options...
Recommended Posts