Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Instancja klasy w metodzie statycznej.
marcio
post 29.03.2010, 11:57:36
Post #1





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Witam mam nurtujacy problem, a miedzy innymi mam klase ktore tworzy sciezki za pomoca glownych sciezek z config'u.
Teraz mam klase Loader a dokladnie:
  1. <?php
  2. require_once('libraries/Path.php');
  3.  
  4. class Loader {
  5.  
  6.  
  7. private static $libraries = array();
  8.  
  9. //public function __construct() {}
  10.  
  11. public static function load($lib) {
  12.  
  13. $ValidLibs = array(
  14.  
  15. 'Router',
  16. 'View',
  17. 'Cfg'
  18.  
  19. );
  20.  
  21. if(in_array($lib, $ValidLibs) && file_exists('libraries/'.$lib.'.php')) {
  22.  
  23. require_once('libraries/'.$lib.'.php');
  24.  
  25. if(empty(self::$libraries[$lib])) {
  26.  
  27. self::$libraries[$lib] = new $lib();
  28.  
  29. }
  30.  
  31. return self::$libraries[$lib];
  32.  
  33. }
  34.  
  35. }
  36.  
  37.  
  38. public static function Library($lib) {
  39.  
  40. if(file_exists('libraries/'.$lib.'.php')) {
  41.  
  42. require_once('libraries/'.$lib.'.php');
  43. return new $lib();
  44.  
  45. }
  46.  
  47. }
  48.  
  49.  
  50. public static function Helper($helper) {
  51.  
  52. if(file_exists('helpers/'.$helper.'.php')) {
  53.  
  54. require_once('helpers/'.$helper.'.php');
  55.  
  56. }
  57.  
  58. }
  59.  
  60.  
  61. public static function Widget($widget) {
  62.  
  63. if(file_exists('widgets/'.$widget.'.php')) {
  64.  
  65. require_once('widgets/'.$widget.'.php');
  66. $widget = new $widget();
  67.  
  68. if($widget instanceof IWidget_Controller && method_exists($widget, 'Render')) {
  69.  
  70. return $widget -> Render();
  71.  
  72. }
  73.  
  74. return false;
  75.  
  76. }
  77.  
  78. return false;
  79.  
  80. }
  81.  
  82. }
  83.  
  84. ?>

Jednak jak widac ma ona ustawione sciezki na sztywno, a w klasie Path mam sciezki generowane dynamicznie problem polega na tym ze jak stworze instanjce obiektu Path wewnatrz metody statycznej to nie zadziala.
Ma ktos pomysl jak moglbym to rozwiazac?
Probolwalem juz tworzyc instancje za pomoca statycznej metody jednak nie dziala chyba ze cos zle robie.

P.S to jest klasa Path:
  1. <?php
  2.  
  3. class Path {
  4.  
  5.  
  6. protected $cfg;
  7.  
  8. protected $path = array();
  9.  
  10.  
  11. public function __construct() {
  12.  
  13. $this -> cfg = Loader::load('Cfg');
  14.  
  15. $this -> path = array(
  16.  
  17. 'DIR_CTRL' => $this -> cfg -> PathControllers,
  18. 'DIR_MODELS' => $this -> cfg -> PathModels,
  19. 'DIR_VIEWS' => $this -> cfg -> PathViews,
  20. 'DIR_PLUGINS_CONFIG' => $this -> cfg -> PathPlugins.$this -> cfg -> PathConfig,
  21. 'DIR_PLUGINS_CTRL' => $this -> cfg -> PathPlugins.$this -> cfg -> PathControllers,
  22. 'DIR_PLUGINS_MODELS' => $this -> cfg -> PathPlugins.$this -> cfg -> PathModels,
  23. 'DIR_PLUGINS_VIEWS' => $this -> cfg -> PathPlugins.$this -> cfg -> PathViews,
  24. 'DIR_PLUGINS_FILTERS' => $this -> cfg -> PathPlugins.$this -> cfg -> PathFilters,
  25. 'DIR_COMPONENTS_CONFIG' => $this -> cfg -> PathModules.$this -> cfg -> PathConfig,
  26. 'DIR_COMPONENTS_CTRL' => $this -> cfg -> PathModules.$this -> cfg -> PathControllers,
  27. 'DIR_COMPONENTS_MODELS' => $this -> cfg -> PathModules.$this -> cfg -> PathModels,
  28. 'DIR_COMPONENTS_VIEWS' => $this -> cfg -> PathModules.$this -> cfg -> PathViews,
  29. 'DIR_WIDGETS_CTRL' => $this -> cfg -> PathWidgets.$this -> cfg -> PathControllers,
  30. 'DIR_LIBRARY' => $this -> cfg -> PathLibs,
  31. 'DIR_HELPERS' => $this -> cfg -> PathHelpers,
  32. 'DIR_LANG' => $this -> cfg -> PathLang,
  33. 'DIR_INTERFACES' => $this -> cfg -> PathInterfaces,
  34. 'DIR_CACHE' => $this -> cfg -> PathCache,
  35. 'DIR_CONFIG' => $this -> cfg -> PathConfig
  36.  
  37. );
  38.  
  39. }
  40.  
  41.  
  42. public function get() {
  43.  
  44. return $this -> path;
  45.  
  46. }
  47.  
  48.  
  49. public function __get($key) {
  50.  
  51. if(file_exists($this ->path[strtoupper($key)]))
  52. return $this -> path[strtoupper($key)];
  53.  
  54. return false;
  55.  
  56. }
  57.  
  58.  
  59. }
  60.  
  61. ?>


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
zzeus
post 29.03.2010, 12:33:13
Post #2





Grupa: Zarejestrowani
Postów: 441
Pomógł: 71
Dołączył: 3.09.2007
Skąd: wrocław

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


Stwórz instancje klasy Path poza klasą Loader, i później przekaż ją do metody w której będziesz jej używał.


--------------------
Go to the top of the page
+Quote Post
marcio
post 29.03.2010, 13:43:37
Post #3





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Cytat(zzeus @ 29.03.2010, 13:33:13 ) *
Stwórz instancje klasy Path poza klasą Loader, i później przekaż ją do metody w której będziesz jej używał.

Wlasnie przed chwila robilem taki misz masz ale cos nie chcialo dzialac czekaj sproboje jeszcze raz mialem przerwe w php wiec moze cos zamotalem.

P.S problem w tym ze zbyt czesto ja uzywam musialbym duzo plikow edytowac.

Znowu w pliku konfiguracji mam stale i te metody statyczne ich nie widza ROTFL z innymi klasami nie mam takiego problemu.

Znow jak zrobie tak:
  1. $cfg = Loader::load('Cfg');
  2. echo(DIR_LIBRARY);
  3.  
  4.  
  5. class Loader {
  6.  
  7.  
  8. private static $libraries = array();
  9.  
  10. public static function load($lib) {
  11.  
  12. $ValidLibs = array(
  13.  
  14. 'Router',
  15. 'View',
  16. 'Cfg'
  17.  
  18. );
  19.  
  20. if(in_array($lib, $ValidLibs) && file_exists(DIR_LIBRARY.$lib.'.php')) {
  21.  
  22. require_once(DIR_LIBRARY.$lib.'.php');
  23.  
  24. if(empty(self::$libraries[$lib])) {
  25.  
  26. self::$libraries[$lib] = new $lib();
  27.  
  28. }
  29.  
  30. return self::$libraries[$lib];
  31.  
  32. }
  33.  
  34. }

To stala nad klasa sie wyswietla ale wewnatrz klasy nie jest juz widoczna.Wywala blad ktory nie ma nic wspolnego z ta metoda.Jak ja wywolam w metodzie load to znowu mam pusta strone nawet z error_reporting(E_ALL); nic trudno chyba w tej klasie sciezki beda ustawione na sztywno :/

Nic probowalem wszystkiego trudno beda statyczne sciezki w tym pliku dzieki za pomoc.

Ten post edytował marcio 29.03.2010, 12:39:44


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
-=Peter=-
post 29.03.2010, 14:36:03
Post #4





Grupa: Zarejestrowani
Postów: 304
Pomógł: 51
Dołączył: 4.02.2005
Skąd: Kraków

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


W jaki sposób stała DIR_LIBRARY ma być widoczna w klasie Loader przed tym jak konfiguracja (zakładam że w klasie Config tworzysz tą stałą) została wczytana? Innymi słowy wywołujesz Loader::load('Config'), a w metodzie load używasz stałej która jest definiowana jak mniemam w klasie którą w tym momencie ładujesz. Klasy konfiguracyjne ładuj poprzez include/require, bo zazwyczaj klasa loadera używa danych z konfiguracji.


--------------------
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: 13.06.2025 - 04:46