当前位置:Gxlcms > PHP教程 > php文件名和类名相同时include不起作用?

php文件名和类名相同时include不起作用?

时间:2021-07-01 10:21:17 帮助过:2人阅读

文件名:原来为ValidationCode.php
修改为validate.php后可以正常显示验证码,
浏览器中请求code.php文件中进行显示验证码

  1. <code><!--?php
  2. class ValidationCode{
  3. private $width;
  4. private $height;
  5. private $num_chars;
  6. private $image;
  7. const BORDER = 1;
  8. function __construct($width=60,$height=20,$num_chars=4){
  9. $this--->width = $width;
  10. $this->height = $height;
  11. $this->num_chars = $num_chars;
  12. }
  13. function showImage(){
  14. $this->createImage();
  15. $this->drawBorder();
  16. $this->drawChars();
  17. $this->outPic();
  18. }
  19. //create canvas
  20. function createImage(){
  21. $this->image = imagecreate($this->width,$this->height);
  22. //$rand_color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  23. $back = imagecolorallocate($this->image,0,0,0);
  24. $border = imagecolorallocate($this->image,255,255,255);
  25. imagefill($this->image,0,0,$back);
  26. //imagefilledrectangle($thid->image,self::BORDER,self::BORDER,$this->width-self::BORDER,$this->height-self::BORDER,$border);
  27. //imageline($this->image,1,1,100,100,$rand_color);
  28. //$this->outPic();
  29. }
  30. //draw border
  31. private function drawBorder(){
  32. $outer_bg_color = imagecolorallocate($this->image,0,0,0);
  33. $inner_bg_color = imagecolorallocate($this->image,255,255,255);
  34. imagefill($this->image,0,0,$outer_bg_color);
  35. imagefilledrectangle($this->image,self::BORDER,self::BORDER,$this->width-self::BORDER-1,$this->height-self::BORDER-1,$inner_bg_color);
  36. }
  37. //create char content
  38. function createChar(){
  39. $rand_ascii="";
  40. $rand_type = rand(0,2);
  41. switch($rand_type){
  42. case 0:
  43. $rand_ascii = rand(48,57);
  44. break;
  45. case 1:
  46. $rand_ascii = rand(65,90);
  47. break;
  48. case 2:
  49. $rand_ascii = rand(97,122);
  50. break;
  51. }
  52. $rand_str = sprintf("%c",$rand_ascii);
  53. return $rand_str;
  54. }
  55. //draw char
  56. private function drawChars(){
  57. $x = $this->width/$this->num_chars+1;
  58. $y = $this->height/2;
  59. for($index = 0; $index<$this->num_chars; $index++){
  60. $rand_color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  61. imagechar($this->image,3,$x*$index+2,$y-$y/2,$this->createChar(),$rand_color);
  62. }
  63. }
  64. //out pic
  65. private function outPic(){
  66. header("content-type:image/png");
  67. imagepng($this->image);
  68. }
  69. function __destruct(){
  70. imagedestroy($this->image);
  71. }
  72. }
  73. /*$code = new ValidationCode();
  74. //echo $code->createChar();
  75. $code->showImage();*/
  76. ?></code>

code.php

  1. <code><!--?php
  2. require_once("validate.php");
  3. $code = new ValidationCode();
  4. //echo $code--->createChar();
  5. $code->showImage();
  6. ?></code>

这个问题已被关闭,原因:已经自己找到问题原因

回复内容:

文件名:原来为ValidationCode.php
修改为validate.php后可以正常显示验证码,
浏览器中请求code.php文件中进行显示验证码

  1. <code><!--?php
  2. class ValidationCode{
  3. private $width;
  4. private $height;
  5. private $num_chars;
  6. private $image;
  7. const BORDER = 1;
  8. function __construct($width=60,$height=20,$num_chars=4){
  9. $this--->width = $width;
  10. $this->height = $height;
  11. $this->num_chars = $num_chars;
  12. }
  13. function showImage(){
  14. $this->createImage();
  15. $this->drawBorder();
  16. $this->drawChars();
  17. $this->outPic();
  18. }
  19. //create canvas
  20. function createImage(){
  21. $this->image = imagecreate($this->width,$this->height);
  22. //$rand_color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  23. $back = imagecolorallocate($this->image,0,0,0);
  24. $border = imagecolorallocate($this->image,255,255,255);
  25. imagefill($this->image,0,0,$back);
  26. //imagefilledrectangle($thid->image,self::BORDER,self::BORDER,$this->width-self::BORDER,$this->height-self::BORDER,$border);
  27. //imageline($this->image,1,1,100,100,$rand_color);
  28. //$this->outPic();
  29. }
  30. //draw border
  31. private function drawBorder(){
  32. $outer_bg_color = imagecolorallocate($this->image,0,0,0);
  33. $inner_bg_color = imagecolorallocate($this->image,255,255,255);
  34. imagefill($this->image,0,0,$outer_bg_color);
  35. imagefilledrectangle($this->image,self::BORDER,self::BORDER,$this->width-self::BORDER-1,$this->height-self::BORDER-1,$inner_bg_color);
  36. }
  37. //create char content
  38. function createChar(){
  39. $rand_ascii="";
  40. $rand_type = rand(0,2);
  41. switch($rand_type){
  42. case 0:
  43. $rand_ascii = rand(48,57);
  44. break;
  45. case 1:
  46. $rand_ascii = rand(65,90);
  47. break;
  48. case 2:
  49. $rand_ascii = rand(97,122);
  50. break;
  51. }
  52. $rand_str = sprintf("%c",$rand_ascii);
  53. return $rand_str;
  54. }
  55. //draw char
  56. private function drawChars(){
  57. $x = $this->width/$this->num_chars+1;
  58. $y = $this->height/2;
  59. for($index = 0; $index<$this->num_chars; $index++){
  60. $rand_color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  61. imagechar($this->image,3,$x*$index+2,$y-$y/2,$this->createChar(),$rand_color);
  62. }
  63. }
  64. //out pic
  65. private function outPic(){
  66. header("content-type:image/png");
  67. imagepng($this->image);
  68. }
  69. function __destruct(){
  70. imagedestroy($this->image);
  71. }
  72. }
  73. /*$code = new ValidationCode();
  74. //echo $code->createChar();
  75. $code->showImage();*/
  76. ?></code>

code.php

  1. <code><!--?php
  2. require_once("validate.php");
  3. $code = new ValidationCode();
  4. //echo $code--->createChar();
  5. $code->showImage();
  6. ?></code>

测试:入口代码

  1. <code><!--?php
  2. require_once("ValidationCode.php");//include_once("ValidationCode.php");
  3. $code = new ValidationCode();
  4. //echo $code--->createChar();
  5. $code->showImage();</code>

文件名:

测试结果:

结论:include的时候文件名跟类名没有关系。结果是可以的。我不知道你的为什么不可以。但是我测了是可以的。

真的假的啊!文件名和类名一样引用不起作用,这么神奇!有没有出什么错误提示

人气教程排行