我的方块赢了';你不能在后台显示php吗?

我的方块赢了';你不能在后台显示php吗?,php,class,object,Php,Class,Object,我修复了一些东西,但屏幕上仍然没有显示任何内容,只是空白,没有错误,什么都没有。 这与我如何归还图纸有关吗? 在LinuxMint13上的终端上运行它,首先出现了这个问题header('Content-type:image/png')那么位置可能是什么?这应该与return语句一起使用吗?我应该使用include或require创建一个文件来引入新的方块吗 <?php header ('Content-type: image/png'); class Shape { private $

我修复了一些东西,但屏幕上仍然没有显示任何内容,只是空白,没有错误,什么都没有。 这与我如何归还图纸有关吗? 在LinuxMint13上的终端上运行它,首先出现了这个问题
header('Content-type:image/png')那么位置可能是什么?这应该与return语句一起使用吗?我应该使用include或require创建一个文件来引入新的方块吗

<?php
header ('Content-type: image/png');

class Shape {

private $size = 0;

public function __construct() {
  $foregroundRhex = substr($foregroundhex,0,2);
  $foregroundGhex = substr($foregroundhex,2,2);
  $foregroundBhex = substr($foregroundhex,4,2);

  $foregroundRed = hexdec($foregroundRhex);
  $foregroundGreen = hexdec($foregroundGhex);
  $foregroundBlue = hexdec($foregroundBhex);

  $foregroundcolor = imagecolorallocate($canvas, $foregroundRed,$foregroundGreen,        $foregroundBlue);
  $backgroundcolor = imagecolorallocate($canvas, $backgroundRed, $backgroundGreen, $backgroundBlue);
}

function draw(){
return 
    $canvas = imagecreatetruecolor($size, $size) or die('Cannot Initialize the Image Stream Yoh!');
    $correctpos = ($size - $position);
    imagerectangle($canvas, $correctpos, $correctpos, $length, $length, $foregroundcolor);
    imagefill($canvas, 0, 0, $backgroundcolor); 
    imagepng($canvas);
    imagedestroy($canvas);
 }
}
class Square extends Shape {

function __construct($x, $y) {
    $this->size = $x;
    $this->size = $y;
}

function draw() {
    return 
    $canvas = imagecreatetruecolor($size, $size) or die('Cannot Initialize the Image Stream Yoh!');
    $correctpos = ($size - $position);
    imagerectangle($canvas, $correctpos, $correctpos, $length, $length, $foregroundcolor);
    imagefill($canvas, 0, 0, $backgroundcolor); 
    imagepng($canvas);
    imagedestroy($canvas);
 }
}

$shapes = array(
new Square(150, '000099', 'ff0000', 5, 25), 
new Square(150, '009900', 'ff0000', 4, 19)
);

?>

您的错误是因为类可能只包含属性和方法定义。因此,在这种情况下,您需要做的是将所有代码封装在一个方法(可能是构造函数)中,如下所示:

在一个不是方法(函数)或属性(变量)定义的一部分的类中,不能有可执行代码。如果您认为您只是在定义属性:您只能将编译时常量值定义为属性默认值。如果需要更多的动态值(如子字符串等),则需要在_构造()或中定义它。

$foregroundRhex=substr($foregroundhex,0,2); 这和它后面的7行需要在函数(方法)或构造函数内部。它们现在所处的空间仅用于类范围声明。

$shapes=array(新正方形(150,'000099','ff0000',5,25),新正方形(150,'009900','ff0000',4,19);)需要是:
$shapes=array(新正方形(150,'000099','ff0000',5,25),新正方形(150,'009900','ff0000',4,19))
class Shape {
   private $size = 0;

   public function __construct() {
      $foregroundRhex = substr($foregroundhex,0,2);
      $foregroundGhex = substr($foregroundhex,2,2);
      $foregroundBhex = substr($foregroundhex,4,2);

      $foregroundRed = hexdec($foregroundRhex);
      $foregroundGreen = hexdec($foregroundGhex);
      $foregroundBlue = hexdec($foregroundBhex);

      $foregroundcolor = imagecolorallocate($canvas, $foregroundRed,$foregroundGreen,        $foregroundBlue);
      $backgroundcolor = imagecolorallocate($canvas, $backgroundRed, $backgroundGreen, $backgroundBlue);
   }

   function draw();
   }
}