nemakuphp Posted January 17, 2010 Report Share Posted January 17, 2010 Ir apmēram šāda struktūra. Kā panākt, lai $variable 5 būtu pieejams arī citām klasēm? Un teiksim, ja man citās klasēs arī ir $variable5, ir iespējams kaut kā apkopot visus tos $variable5 no visām klasēm? <?php class register{ private function function1($variable1){ //te if/else sūdi return $variable2; } public function function2($variable3){ $variable4 = $this->function1($variable3); if(!is_array($variable4)){ } elseif($variable4['lauks'] == 'vertiba1'){ $variable5 = 'vertiba1'; } elseif($variable4['lauks'] == 'vertiba2'){ $variable5 = 'vertiba2'; } } } ?> Quote Link to comment Share on other sites More sharing options...
briedis Posted January 17, 2010 Report Share Posted January 17, 2010 (edited) class SomeClass{ private $var1; public function func1(){ $this->var1 = 10; } public function func2(){ return $this->var1; } } Palasi šitos: http://php.net/manual/en/language.oop5.php Edited January 17, 2010 by briedis Quote Link to comment Share on other sites More sharing options...
nemakuphp Posted January 17, 2010 Author Report Share Posted January 17, 2010 Laikam nesaprati ko vēlos, bet nu nekas, varbūt ne tā uzrakstīju. Pieņemsim, ka man ir šī klase. Es viņu includoju, piemēram, index failā. Veicu funkciju2 ja ir tas variablis5, tad funkcija neko neatgriež (koda fragmentā tas nav). Kā lai pēctam, kad funkcija izpildīta, tieku klāt tam variablim5 un vai tas vispār ir iespējams? Quote Link to comment Share on other sites More sharing options...
briedis Posted January 17, 2010 Report Share Posted January 17, 2010 <?php class Klase{ private $var1; function __construct(){ $var1 = 1; } public function add(){ $this->var1++; } public function getVar(){ return $this->var1; } } $klase = new Klase(); echo $klase->getVar(); $klase->add(); echo $klase->getVar(); ?> Quote Link to comment Share on other sites More sharing options...
nemakuphp Posted January 17, 2010 Author Report Share Posted January 17, 2010 Un kā es varētu izmantot citas klases funkciju šajā klasē? Quote Link to comment Share on other sites More sharing options...
2easy Posted January 17, 2010 Report Share Posted January 17, 2010 palasi par inheritance (mantošana) Quote Link to comment Share on other sites More sharing options...
nemakuphp Posted January 17, 2010 Author Report Share Posted January 17, 2010 Mēģināšu palasīt Quote Link to comment Share on other sites More sharing options...
Java Posted January 17, 2010 Report Share Posted January 17, 2010 Mainīgo klasē definē kā privātu, piekļūt no ārienes vari tam ar publiskiem "getteriem" un "setteriem", kā jau tev te paskaidroja. Ievēro labo stilu un nodefinē mainīgo, vienalga vai tas būtu "temporary variable" vai "object field" vai globālais mainīgais (php "fīča"). Quote Link to comment Share on other sites More sharing options...
nemakuphp Posted January 18, 2010 Author Report Share Posted January 18, 2010 Paldies visiem par palīdzību. Beigās paņēmu jau gatavas klases, papētīju un ieguvu sev vēlamo rezultātu. Varat slēgt ciet. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.