Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> 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

Posty w temacie


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: 19.06.2025 - 22:27