Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Rozszerzanie klas
joytec
post
Post #1





Grupa: Zarejestrowani
Postów: 66
Pomógł: 0
Dołączył: 24.06.2005

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


Zastanawiam sie czy mozna rozszerzyc classe o klase ktora znajduje sie w innym katalogu np podrzednym zazaczam ze stosuja funcje function __autoload($className) w ktorej sa warunki jesli klasa znajduje sie w katalogu a to zaladuj jesli b to zaladuj z katalogu b

konkretnie

chce rozszerzyc clsse a o b z tym ze classa a znajduje sie w katalogu a, klasa b w podkatalogu b w katalogu a


Prosze o jakies sugestie

Ten post edytował joytec 19.03.2010, 09:26:08
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
starach
post
Post #2





Grupa: Zarejestrowani
Postów: 999
Pomógł: 30
Dołączył: 14.01.2007
Skąd: wiesz ?

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


To ja też dorzucę trochę swoich mOndrości. ;p

Chcesz dobry autoloader? Zainteresuj się przestrzeniami nazw w PHP 5.3. Możesz podejrzeć kod autoloadera w symfony 2.0, który z tego korzysta i jest właściwie największą siłą nowej wersji tego frameworka.

Albo zresztą żebyś nie musiał grzebać. ;p

  1. <?php
  2.  
  3. namespace Symfony\Foundation;
  4.  
  5. /*
  6.  * This file is part of the symfony package.
  7.  *
  8.  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  9.  *
  10.  * For the full copyright and license information, please view the LICENSE
  11.  * file that was distributed with this source code.
  12.  */
  13.  
  14. /**
  15.  * ClassLoader implementation that implements the technical interoperability
  16.  * standards for PHP 5.3 namespaces and class names.
  17.  *
  18.  * Based on <a href="http://groups.google.com/group/php-standards/web/psr-0-final-proposal" target="_blank">http://groups.google.com/group/php-standar...-final-proposal</a>
  19.  *
  20.  * Example usage:
  21.  *
  22.  * [php]
  23.  * $loader = new ClassLoader();
  24.  * $loader->registerNamespace('Symfony', __DIR__.'/..');
  25.  * $loader->register();
  26.  *
  27.  * @author Jonathan H. Wage <jonwage@gmail.com>
  28.  * @author Roman S. Borschel <roman@code-factory.org>
  29.  * @author Matthew Weier O'Phinney <matthew@zend.com>
  30.  * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  31.  * @author Fabien Potencier <fabien.potencier@symfony-project.org>
  32.  */
  33. class ClassLoader
  34. {
  35. protected $namespaces = array();
  36.  
  37. /**
  38.   * Creates a new loader for classes of the specified namespace.
  39.   *
  40.   * @param string $namespace The namespace to use
  41.   * @param string $includePath The path to the namespace
  42.   */
  43. public function registerNamespace($namespace, $includePath = null)
  44. {
  45. $this->namespaces[$namespace] = $includePath;
  46. }
  47.  
  48. /**
  49.   * Installs this class loader on the SPL autoload stack.
  50.   */
  51. public function register()
  52. {
  53. spl_autoload_register(array($this, 'loadClass'));
  54. }
  55.  
  56. /**
  57.   * Loads the given class or interface.
  58.   *
  59.   * @param string $className The name of the class to load
  60.   */
  61. public function loadClass($className)
  62. {
  63. $vendor = substr($className, 0, stripos($className, '\\'));
  64. if (!isset($this->namespaces[$vendor]))
  65. {
  66. return;
  67. }
  68.  
  69. if (false !== ($lastNsPos = strripos($className, '\\')))
  70. {
  71. $namespace = substr($className, 0, $lastNsPos);
  72. $className = substr($className, $lastNsPos + 1);
  73. $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
  74. }
  75. else
  76. {
  77. $namespace = '';
  78. $fileName = '';
  79. }
  80. $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
  81.  
  82. require $this->namespaces[$vendor].DIRECTORY_SEPARATOR.$fileName;
  83. }
  84. }
- Prostszego i wydajniejszego autoloadera to jeszcze nie widziałem...
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: 11.10.2025 - 21:43