Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [OOP] jak napisać taką metodę
nu_moon
post
Post #1





Grupa: Zarejestrowani
Postów: 69
Pomógł: 0
Dołączył: 26.01.2006

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


Witam,

Może mnie ktoś naprowadzić w jaki sposób mogę napisać metodę z której będzie można korzystać w taki sposób:

  1. $obiekt->metoda('zmienna') = 'string lub zmienna';
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
NuLL
post
Post #2





Grupa: Zarejestrowani
Postów: 2 262
Pomógł: 21
Dołączył: 3.05.2004
Skąd: Sopot, Krakow, W-wa

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


  1. <?php
  2.  
  3. class FooBar{
  4.  
  5. protected $data=array();
  6.  
  7. public function __set($name,$value){
  8.  
  9. return method_exists($this,'set'.ucfirst($name)) ? $this->{'set'.ucfirst($name)}($value) : $this->data[$name]=$value;
  10. }
  11.  
  12. protected function setFoo($value){
  13.  
  14. return $this->data['foo']=strtolower($value);
  15. }
  16.  
  17. public function __get($name){
  18.  
  19. return isset($this->data[$name])? $this->data[$name] : NULL;
  20. }
  21. }
  22.  
  23. $foobar=new FooBar();
  24.  
  25. $foobar->bar='BAR';
  26.  
  27. echo $foobar->bar; // BAR
  28.  
  29. $foobar->foo='FOO';
  30.  
  31. echo $foobar->foo; // foo

A tak nie wystarczy ? (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 15.10.2025 - 12:23