Zoom Posted September 20, 2006 Report Share Posted September 20, 2006 <?php class klase_1{ function class1_func($mainigais_clas1){ echo $this->mainigais_clas1; return $this->mainigais_clas1; } } $klase = &new klase_1(); class klase_2{ function klase_divi{ //man jālieto ir pirmas klases funkcija $klase->class1_func(); } } $klass2 = &new klase_2(); $klass2->klase_divi; //jaizvada 1 kalses funkciju ?> Pareizi es to daru? Link to comment Share on other sites More sharing options...
konrads Posted September 20, 2006 Report Share Posted September 20, 2006 <?php class klase_1{ function class1_func($mainigais_clas1){ echo $this->mainigais_clas1; return $this->mainigais_clas1; } } $klase = &new klase_1(); class klase_2{ function klase_divi{ //man jālieto ir pirmas klases funkcija $klase->class1_func(); } } $klass2 = &new klase_2(); $klass2->klase_divi; //jaizvada 1 kalses funkciju ?> Pareizi es to daru? Jautājums bez papildus informācijas nav atbildāms. Ja class1_func neizmanto parametrus no class1, tad deklarē static un miers Citādi, $c1 = new class1(); $c1->f1(); Principā, case of bad design, pārdomā, ko tu dari :) Link to comment Share on other sites More sharing options...
Vebers Posted September 20, 2006 Report Share Posted September 20, 2006 A class can inherit methods and members of another class by using the extends keyword in the declaration. It is not possible to extend multiple classes, a class can only inherit one base class. http://lv.php.net/manual/en/language.oop5.basic.php Link to comment Share on other sites More sharing options...
Zoom Posted September 20, 2006 Author Report Share Posted September 20, 2006 <?php class A{ function foo($aaa){ echo $this->aaa; return $this->aaa; } } class B{ function bar(){ A::foo("aa"); } } $a = new A(); $a->foo(); echo '<br>'; $b = new B(); $b->bar(); ?> Warning: Missing argument 1 for A::foo(), called in C:\Program Files\Apache Group\Apache2\htdocs\priv\serveris\macibu\oop\test_class_in_class.php on line 21 and defined in C:\Program Files\Apache Group\Apache2\htdocs\priv\serveris\macibu\oop\test_class_in_class.php on line 5 Notice: Undefined property: A::$aaa in C:\Program Files\Apache Group\Apache2\htdocs\priv\serveris\macibu\oop\test_class_in_class.php on line 7 Notice: Undefined property: A::$aaa in C:\Program Files\Apache Group\Apache2\htdocs\priv\serveris\macibu\oop\test_class_in_class.php on line 8 Notice: Undefined property: B::$aaa in C:\Program Files\Apache Group\Apache2\htdocs\priv\serveris\macibu\oop\test_class_in_class.php on line 7 Notice: Undefined property: B::$aaa in C:\Program Files\Apache Group\Apache2\htdocs\priv\serveris\macibu\oop\test_class_in_class.php on line 8 Link to comment Share on other sites More sharing options...
Paulinjsh Posted September 20, 2006 Report Share Posted September 20, 2006 (edited) loģiski, jo tavai klasei A nav tāds klases mainīgais aaa Edited September 20, 2006 by Paulinjsh Link to comment Share on other sites More sharing options...
bubu Posted September 20, 2006 Report Share Posted September 20, 2006 Zoom: lūdzu izdari pakalpojumu visiem - neprogrammē OOP (prasīt neprogrammēt vispār laikam ir bezjēdzīgi). Tu tka nesaproti, ko nozīmē -> ne arī :: Tik uz dullo visu mēģini.. Atver kādu tutoriāli tak un palasi, kautvai to pašu manuāli. Link to comment Share on other sites More sharing options...
Delfins Posted September 21, 2006 Report Share Posted September 21, 2006 class Printer { function printer(& $item) { $this->item = $item; } function print() { echo $this->item->getContent(); } } PS: ti ktiešām, bez PHP/OOP sintakses zināšanas un specifikas tālu netiksi ar tiem objektiem Link to comment Share on other sites More sharing options...
john.brown Posted September 21, 2006 Report Share Posted September 21, 2006 (edited) Var dažādi, atkarībā, ko vajag :) Tikai ne tā, kā tev ir darīts. Un, ja gribi oop, lieto php5. ātrāk pie skaidrības varbūt tiksi... class Klase_1{ private $mainigais_clas1 = NULL; public function __construct() { $this->mainigais_clas1 = 'Klases 1 mainigais'; } public function class1_func(){ echo $this->mainigais_clas1; return $this->mainigais_clas1; } } class Klase_2{ private $class1_obj = NULL; public function __construct(Klase_1 $class1_obj) { $this->class1_obj = $class1_obj; } public function class2_func() { //man jālieto ir pirmas klases funkcija $this->class1_obj->class1_func(); } } $klass2 = new Klase_2(new Klase_1()); $klass2->class2_func(); //jaizvada 1 klases funkciju class Klase_1{ private $mainigais_clas1 = NULL; public function __construct() { $this->mainigais_clas1 = 'Klases 1 mainigais'; } public function class1_func(){ echo $this->mainigais_clas1; return $this->mainigais_clas1; } } class Klase_2 extends Klase_1{ public function class2_func() { //man jālieto ir pirmas klases funkcija $this->class1_func(); } } $class2 = new Klase_2(); $class2->class2_func(); // izvada pirms klases funkciju $class2->class1_func(); // arī izvada pirmās klases funkciju;) Ak jā, un ja jēgā gribi tikt, tad definē tomēr katrai klasei mainīgos sākumā, un nevis lieto fīču, kura ļauj šos radīt on the fly Edited September 21, 2006 by john.brown Link to comment Share on other sites More sharing options...
Zoom Posted September 21, 2006 Author Report Share Posted September 21, 2006 Paldies Vīri ;) Link to comment Share on other sites More sharing options...
Recommended Posts