Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> php 5.3 router
aras785
post
Post #1





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

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


Witam. Uczę się php 5.3 i przerabiam MVC, który ostatnio napisałem (wrzuciłem na forum) i mam pytanie dotyczące use i namespace.
Jak sprawdzić czy klasa istnieje? Mam to ogarnąc przez: spl_autoload_register i tam zrobić np. przekierowanie do strony z bledem?
To co niżej podałem to nwywala wyjątek z spl...
  1. <?php
  2. namespace Lib;
  3. class Router {
  4. //private $controller;
  5. //private $action;
  6. public function __construct(Request $request) {
  7. //$this->controller = $request->getController();
  8. //$this->action = $request->getAction();
  9. if($class = new \Application\Controller\Index) {
  10.  
  11. }
  12.  
  13. }
  14. }


Go to the top of the page
+Quote Post
Crozin
post
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


class_exists?
Go to the top of the page
+Quote Post
aras785
post
Post #3





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

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


Nie o to chodzi. Jeśli klasa nie istnieje to wywala mi wyjątek z spl... A mi chodzi aby tego uniknąć i przekierować gdzie indziej. I sprawdzanie typu class_exists dziala ale tylko gdy klasa istnieje jesli nie to error.

I jeszcze daje plik index:

  1. <?php
  2. spl_autoload_extensions(".php");
  3. spl_autoload_register();
  4.  
  5. use Lib\Router;
  6. use Lib\Request;
  7. $application = new Router(new Request());


//Edit

Udało mi się rozwiązać banalny problem... Uparłem się na to spl

  1. <?php
  2. function __autoload($c)
  3. {
  4. $file = $c.".php";
  5. if(file_exists($file)) {
  6. require_once $file;
  7. }
  8. }
  9.  
  10. use Lib\Router;
  11. use Lib\Request;
  12. $application = new Router(new Request());
  13.  


Dzięki:)

Ten post edytował aras785 23.02.2013, 16:02:45
Go to the top of the page
+Quote Post
sazian
post
Post #4





Grupa: Zarejestrowani
Postów: 1 045
Pomógł: 141
Dołączył: 19.09.2006
Skąd: B-tów

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


  1. function ClassLoader($classname)
  2. {
  3. $file=$classname.'.php';
  4. if(file_exists($file) && is_readable($file))
  5. {
  6. require_once($file);
  7. if(class_exists($classname, false))
  8. {
  9. return true;
  10. }
  11. }
  12. return false;
  13. }
  14.  
  15. spl_autoload_register('ClassLoader');
  16.  
  17.  
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: 18.09.2025 - 10:51