Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php(5)]Klasy
Eagle
post
Post #1





Grupa: Zarejestrowani
Postów: 170
Pomógł: 14
Dołączył: 16.03.2007

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


Witam (ponownie (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) )
Ostatnio postanowiłem przepisać swoje projekty na obiektowe.
Jednak już na samym początku mam problem:

index.php5
  1. <?php
  2. include ('plug.php5');
  3.  
  4.  
  5. $c_name = 'UserInfo';
  6.  
  7. call_user_func(array($c_name, 'main'));
  8. ?>


plug.php5
  1. <?php
  2. class UserInfo
  3. {
  4.  var $imie, $email;
  5.  
  6.  
  7. function __construct()
  8. {
  9. $this->imie='Twoje imię';
  10. $this->email='Twój email';
  11. }
  12.  
  13.  function main()
  14.  {  
  15. echo $this->imie;
  16. echo $this->email;  
  17.  }
  18. }
  19. ?>


Wyskakuje błąd:

Fatal error: Using $this when not in object context in /plug.php5 on line 15
Tylko że nie wiem co jest źle...
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 9)
pbnan
post
Post #2





Grupa: Zarejestrowani
Postów: 174
Pomógł: 0
Dołączył: 27.03.2007
Skąd: Osiek almost City ;-D

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


Zamień:
  1. <?php
  2. $c_name = 'UserInfo';
  3. ?>

...na:
  1. <?php
  2. $c_name = new UserInfo();
  3. ?>
Go to the top of the page
+Quote Post
Eagle
post
Post #3





Grupa: Zarejestrowani
Postów: 170
Pomógł: 14
Dołączył: 16.03.2007

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


Tylko ja chcę to akurat zrobić dynamicznie aby można było 'instalować' dodatkowe moduły.
Go to the top of the page
+Quote Post
Statozor
post
Post #4





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 23.09.2006

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


Nie siedze w OO ale wygląda na to że nie masz utworzonego żadnego obiektu tej klasy więc nie wie ona co wyświetlić
Go to the top of the page
+Quote Post
Eagle
post
Post #5





Grupa: Zarejestrowani
Postów: 170
Pomógł: 14
Dołączył: 16.03.2007

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


A może ktoś mi podpowie jak to zrobić mając tylko nazwę klasy w zmiennej ?
(Tak jak to chciałem zrobić za 1 razem (?) )
Go to the top of the page
+Quote Post
pbnan
post
Post #6





Grupa: Zarejestrowani
Postów: 174
Pomógł: 0
Dołączył: 27.03.2007
Skąd: Osiek almost City ;-D

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


Poczytaj o Reflection:
http://pl2.php.net/manual/pl/language.oop5.reflection.php
Go to the top of the page
+Quote Post
Eagle
post
Post #7





Grupa: Zarejestrowani
Postów: 170
Pomógł: 14
Dołączył: 16.03.2007

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


Dzięki poczytam

Problem jest już rozwiązany (ale nie wiem czy to jest najfajniejszy sposóB)

Niżej dam źródełko ... może komuś się przyda (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

  1. <?php
  2. $nazwa = 'UserInfo';
  3.  
  4.  $obiekt = new $nazwa;
  5.  $obiekt->main();
  6. ?>
Go to the top of the page
+Quote Post
acztery
post
Post #8





Grupa: Zarejestrowani
Postów: 945
Pomógł: 7
Dołączył: 15.03.2005
Skąd: katowice

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


moze sie myle ale czy w php5 jesr cos takiego jak var?
Go to the top of the page
+Quote Post
Statozor
post
Post #9





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 23.09.2006

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


Jest ale niezalecane.
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #10





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


Nie mozna uzywac $this wewatrz funkcji statycznej, np. w

User::main()

albo uzyj zmiennej statycznej, albo wywolaj operacje na obiekcie.
Go to the top of the page
+Quote Post

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: 24.08.2025 - 15:36