Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dostęp (pobranie) właściwości klasy, po nazwie z parametru, $this->_$property
mrWodoo
post 20.08.2012, 21:58:56
Post #1





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Witam, mam pokaźną ilość własności/właściwości w klasie, nie chce mi się do każdej pisać metody getCośTam, więc uznałem, że sobie to uproszczę metodą i mam problem, metoda wygląda tak
  1. /**
  2. * Get private/protected propoerties (convention: underscore as prefix for private/protected properties)
  3. *
  4. * @access public static
  5. * @param string $member
  6. * @return mixed
  7. */
  8. public static function get( $property )
  9. {
  10. if( property_exists( 'Ceres_Application', '_' . $property ) )
  11. {
  12. return $this->{_.$member};
  13. }
  14. else
  15. {
  16. return false;
  17. }
  18. }


problem jest z return $this->{_.$member}, jakoś nieudolnie próbowałem to skleić ale wyszło mi jak Polsce na E2012


--------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 7)
untorched
post 20.08.2012, 22:09:06
Post #2





Grupa: Zarejestrowani
Postów: 318
Pomógł: 76
Dołączył: 27.12.2011
Skąd: Dąbrowa Górnicza

Ostrzeżenie: (0%)
-----


Cytat
* @param string $member

  1. public static function get( $property )

  1. if( property_exists( 'Ceres_Application', '_' . $property ) )
  2. {
  3. return $this->{_.$member};
  4. }


To $property czy $member?
Go to the top of the page
+Quote Post
mrWodoo
post 20.08.2012, 22:15:25
Post #3





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Sorki, w clipboardzie miałem starą wersję (ale ja głupi jestem), kod nie działa (to co podałeś)


--------------------
Go to the top of the page
+Quote Post
untorched
post 20.08.2012, 22:28:51
Post #4





Grupa: Zarejestrowani
Postów: 318
Pomógł: 76
Dołączył: 27.12.2011
Skąd: Dąbrowa Górnicza

Ostrzeżenie: (0%)
-----


... A teraz?
  1. public function get( $property )
  2. {
  3. if( property_exists( 'Ceres_Application', '_' . $property ) )
  4. {
  5. return $this->{'_' . $property};
  6. }
  7. else
  8. {
  9. return false;
  10. }
  11. }


Ten post edytował untorched 20.08.2012, 22:49:33
Go to the top of the page
+Quote Post
mrWodoo
post 20.08.2012, 22:32:22
Post #5





Grupa: Zarejestrowani
Postów: 160
Pomógł: 6
Dołączył: 13.01.2012
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Dzięki!
Mała poprawka
  1. return $this->{ '_' . $property };


a i moja metoda nei ma być statyczna, nie wiem czemu taką definicję dałem ^^ przecież to nie robi żadnego sensu

Ten post edytował mrWodoo 20.08.2012, 22:35:01


--------------------
Go to the top of the page
+Quote Post
quality
post 12.09.2012, 09:34:10
Post #6





Grupa: Zarejestrowani
Postów: 172
Pomógł: 9
Dołączył: 13.02.2006
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Nie lepiej uzyc magicznej metody __get() , __set() ?

Pozdrawiam


--------------------
Go to the top of the page
+Quote Post
luki100011
post 13.09.2012, 13:17:49
Post #7





Grupa: Zarejestrowani
Postów: 243
Pomógł: 20
Dołączył: 20.04.2004
Skąd: Wielkopolska

Ostrzeżenie: (0%)
-----


Jak używasz Netbeans - a to kliknij
Lewy ALT + Insert i wybierz z menu "tworzenie seterów i geterów"


--------------------
Go to the top of the page
+Quote Post
KotWButach
post 15.09.2012, 13:15:21
Post #8





Grupa: Zarejestrowani
Postów: 361
Pomógł: 10
Dołączył: 8.02.2012

Ostrzeżenie: (0%)
-----


a probowałeś tak?

  1. $result = '_' . $property;
  2. return $this->$result;


powinno zadziałać bo sam tak coś mi się kojarzy... miałem kiedyś taki problem

Ten post edytował KotWButach 15.09.2012, 13:16:51
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 08:41