Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: klasy i referencje
Forum PHP.pl > Forum > PHP
ghostrider
mam taki kod php
  1. <?
  2. class classA {
  3.  
  4. var $objects = array();
  5. var $big_str = "";
  6.  
  7. function classA(){}
  8.  
  9. function create(){
  10. $obj = new classB();
  11. $this->objects[] = $obj;
  12. return $obj;
  13. }
  14.  
  15. function echos(){
  16. foreach ($this->objects as $obj ) {
  17. $this->big_str .= $obj->get_str() ;
  18. }
  19. print $this->big_str;
  20. }
  21.  
  22.  
  23. }
  24.  
  25. class classB {
  26. var $some_string ="";
  27.  
  28. function classB(){}
  29.  
  30. function add($str){
  31. $this->some_string .= $str . "|";
  32. }
  33. function get_str(){
  34. return $this->some_string;
  35. }
  36. }
  37.  
  38. $a = new classA();
  39.  
  40. $b = $a->create();
  41. $b->add("garbage");
  42. $b->add("dummy");
  43. $b->add("some_more");
  44.  
  45. $c = $a->create();
  46. $c->add("here"); 
  47. $c->add("some");
  48. $c->add("text");
  49.  
  50. $a->echos();
  51. #spodziewam się :
  52. #garbage|dummy|some_more|here|some|text|
  53. #a big_str w classie classA jest pusta
  54. var_export($b); 
  55. ?>

i tutaj musi cos byc z referencjami i kopiami obiektów i zmiennych, jednak nie bardzo wiem co. Próbowałem różnych kombinacji, jednak bezskutecznie.
jeśli ktoś może rzucić troche światła na temat referencji i kopi obiektów

z mountain dzienx winksmiley.jpg
ActivePlayer
http://www.sitepoint.com/article/object-oriented-php/7

Cytat
  1. <?php
  2. class Bar {
  3. }
  4.  
  5. class Foo {
  6.  // Return by reference
  7.  function &getBar()
  8.  {
  9.  return new Bar();
  10.  }
  11. }
  12.  
  13. // Instantiate Foo
  14. $foo = &new Foo();
  15.  
  16. // Get an instance of Bar from Foo
  17. $bar = &$foo->getBar();
  18. ?>
ghostrider
no i oto chodzi, krótko, zwięźle i na temat exclamation.gif

dzieki.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.