为什么我可以使用变量代表php中的函数名?

时间:2014-11-15 11:30:08

标签: php

我已经进入了php的开发地狱,一整天都让我困惑的一个错误。 我从以下书籍中搜索示例代码:

  class ThumbnailException extends Exception {
     public function __construct( $message = null, $code = 0 )
     {
         parent::__construct( $message.$code );
         error_log( 'Error in'.$this->getFile().'Line:'.
         $this->getLine().'Error:'.$this->getMessage());
     }
   }
  class ThumbnailFileException extends ThumbnailException {}
  class ThumbnailNotSupportedException extends ThumbnailException {}
  class Thumbnail {
     private $maxHeight;
     private $maxWidth;
     private $scale;
     private $inflate;
     private $types;
     private $imgLoaders;
     private $imgCreators;
     private $source;
     private $sourceWidth;
     private $sourceHeight;
     private $sourceMine;
     private $thumb;
     private $thumbWideth;
     private $thumbHeight;
     public function __construct( $maxWidth, $maxHeight, $scale = true, $inflate )
     {
         $this->maxWidth    = $maxWidth;
         $this->maxHeight   = $maxHeight;
         $this->scale       = $scale;
         $this->inflate     = $inflate;
         $this->types       = array( 'image/jpeg', 'image/png', 'image/gif' );
         $this->imgLoaders  = array(
             'image/jpeg' => 'imagecreatefromjpeg',
             'image/png' => 'imagecreatefrompng',
             'image/gif' => 'imagecreatefromgif' );
         $this->imgCreators = array(
             'image/jpeg' => 'imagejpeg',
             'image/png' => 'imagepng',
             'image/gif' => 'imagegif'
             );
     }
     public function loadFile( $image ) {
         if ( !($dims = @getimagesize($image)))
              {
                  throw new ThumbnailFileException(
                      'Couldn not find image:'.$image );
              }

              if ( in_array( $dims['mime'], $this->types ) )
              {                
                  $loader = $this->imgLoaders[$dims['mime']];
                  echo $dims['mime']. $loader.'hell';
                  $this->source = imagecreatefromjpeg($image);
                  $this->sourceWidth = $dims[0];
                  $this->sourceHeight = $dims[1];
                  $this->initThumb();
                  return(true);
              }else  {
                  throw new ThumbnailNotSupportedException(
                      'Image MINE types '.$dims['mime'].'not supported' );
              }
            }

$tn= new Thumbnail(200,200);
$tn->loadFile('7122559.jpeg');
header('Content-Type: '. $tn->getMine());

在上面的代码中,我将$loader设置为代表函数名称,具体取决于mine类型 $image。我认为代码绝对没有语法错误。 问题是,当我运行上面的代码时,它会给我Function name must be a string错误,但我不知道为什么?当我将$loader更改为与imagecreatefromjpeg这样的确切功能名称时,它会正确运行,但我认为$loaderimagecreatefromjpeg mine时等同于$image 1}} = image / jpeg。也许我有一些PHP的规则不知道,但代码来自一本书,为什么它运行错误?有什么建议吗?谢谢!

0 个答案:

没有答案