php 如何从继承的方法中获取派生类的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3014254/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the path of a derived class from an inherited method?
提问by Jacco
How to get the path of the current class, from an inherited method?
如何从继承的方法中获取当前类的路径?
I have the following:
我有以下几点:
<?php // file: /parentDir/class.php
   class Parent  {
      protected function getDir() {
         return dirname(__FILE__);
      }
   }
?>
and
和
<?php // file: /childDir/class.php
   class Child extends Parent {
      public function __construct() {
         echo $this->getDir(); 
      }
   }
   $tmp = new Child(); // output: '/parentDir'
?>
The __FILE__constant always points to the source-file of the file it is in, regardless of inheritance.
I would like to get the name of the path for the derived class.
该__FILE__常数总是指向它在文件的源文件,无论继承。
我想获取派生类的路径名称。
Is there any elegantway of doing this?
有没有优雅的方法来做到这一点?
I could do something along the lines of $this->getDir(__FILE__);but that would mean that I have to repeat myself quite often. I'm looking for a method that puts all the logic in the parent class, if possible.
我可以做一些类似的事情,$this->getDir(__FILE__);但这意味着我必须经常重复自己。如果可能的话,我正在寻找一种将所有逻辑放在父类中的方法。
Update:
Accepted solution (by Palantir):
更新:
接受的解决方案(由Palantir 提供):
<?php // file: /parentDir/class.php
   class Parent  {
      protected function getDir() {
         $reflector = new ReflectionClass(get_class($this));
         return dirname($reflector->getFileName());
      }
   }
?>
回答by Palantir
Using ReflectionClass::getFileNamewith this will get you the dirname the class Childis defined on.
ReflectionClass::getFileName与此一起使用将为您提供Child定义类的目录名。
$reflector = new ReflectionClass("Child");
$fn = $reflector->getFileName();
return dirname($fn);
You can get the class name of an object with get_class():)
您可以使用get_class():)获取对象的类名
回答by Artefacto
Yes. Building on Palantir's answer:
是的。基于 Palantir 的回答:
   class Parent  {
      protected function getDir() {
         $rc = new ReflectionClass(get_class($this));
         return dirname($rc->getFileName());
      }
   }
回答by Ian Bytchek
Don't forget, since 5.5 you can use classkeyword for the class name resolution, which would be a lot faster than calling get_class($this). The accepted solution would look like this:
不要忘记,从 5.5 开始,您可以使用class关键字来解析类名,这比调用get_class($this). 接受的解决方案如下所示:
protected function getDir() {
    return dirname((new ReflectionClass(static::class))->getFileName());
}
回答by ya.teck
If you are using Composerfor autoloading you can retrieve the directory without reflection.
如果您使用Composer进行自动加载,则无需反射即可检索目录。
$autoloader = require 'project_root/vendor/autoload.php';
// Use get_called_class() for PHP 5.3 and 5.4
$file = $autoloader->findFile(static::class);
$directory = dirname($file);
回答by Adrian
You can also pass the directory as constructor arg. Not super elegant, but at least you don't have to work with reflection or composer.
您还可以将目录作为构造函数 arg 传递。不是超级优雅,但至少您不必使用反射或作曲家。
Parent:
家长:
<?php // file: /parentDir/class.php
   class Parent  {
      private $directory;
      public function __construct($directory) {
          $this->directory = $directory;
      }
      protected function getDir() {
         return $this->directory;
      }
   }
?>
Child:
孩子:
<?php // file: /childDir/class.php
   class Child extends Parent {
      public function __construct() {
        parent::__construct(realpath(__DIR__));
        echo $this->getDir(); 
      }
   }
?>
回答by Remy Mellet
<?php // file: /parentDir/class.php
   class Parent  {
      const FILE = __FILE__;
      protected function getDir() {
         return dirname($this::FILE);
      }
   }
?>
<?php // file: /childDir/class.php
   class Child extends Parent {
      const FILE = __FILE__;
      public function __construct() {
         echo $this->getDir(); 
      }
   }
   $tmp = new Child(); // output: '/childDir'
?>
Please not that if you need to get the dir, directly use __DIR__.
请注意,如果您需要获取目录,请直接使用__DIR__.

