Table of Contents
Reply content:
Home Backend Development PHP Tutorial Include does not work when the php file name and class name are the same?

Include does not work when the php file name and class name are the same?

Jul 06, 2016 pm 01:53 PM
php

File name: Originally ValidationCode.php
After changing to validate.php, the verification code can be displayed normally.
The browser requests the code.php file to display the verification code

<code>&lt;?php
    class ValidationCode{
        private $width;
        private $height;
        private $num_chars;
        private $image;
        const BORDER = 1;

        function __construct($width=60,$height=20,$num_chars=4){
            $this-&gt;width = $width;
            $this-&gt;height = $height;
            $this-&gt;num_chars = $num_chars;
        }
        
        function showImage(){
            $this-&gt;createImage();
            $this-&gt;drawBorder();
            $this-&gt;drawChars();
            $this-&gt;outPic();
        }
        
        //create canvas
         function createImage(){
            $this-&gt;image = imagecreate($this-&gt;width,$this-&gt;height);
            //$rand_color = imagecolorallocate($this-&gt;image,rand(0,255),rand(0,255),rand(0,255));
            $back = imagecolorallocate($this-&gt;image,0,0,0);
            $border = imagecolorallocate($this-&gt;image,255,255,255);
            imagefill($this-&gt;image,0,0,$back);
            //imagefilledrectangle($thid-&gt;image,self::BORDER,self::BORDER,$this-&gt;width-self::BORDER,$this-&gt;height-self::BORDER,$border);
            //imageline($this-&gt;image,1,1,100,100,$rand_color);
            //$this-&gt;outPic();
        }

        //draw border
        private function drawBorder(){
            $outer_bg_color = imagecolorallocate($this-&gt;image,0,0,0);
            $inner_bg_color = imagecolorallocate($this-&gt;image,255,255,255);
            imagefill($this-&gt;image,0,0,$outer_bg_color);
            imagefilledrectangle($this-&gt;image,self::BORDER,self::BORDER,$this-&gt;width-self::BORDER-1,$this-&gt;height-self::BORDER-1,$inner_bg_color);

        }        
    
        //create char content
          function createChar(){
            $rand_ascii="";
            $rand_type = rand(0,2);
            switch($rand_type){
                case 0:
                    $rand_ascii = rand(48,57);  
                   break;
                case 1:
                     $rand_ascii = rand(65,90);
                   break;
                case 2:
                     $rand_ascii = rand(97,122);
                   break;
            }
            $rand_str = sprintf("%c",$rand_ascii);
            return $rand_str;
        }
    
    
    
        //draw char
         private function drawChars(){
            $x = $this-&gt;width/$this-&gt;num_chars+1;
            $y = $this-&gt;height/2;
            for($index = 0; $index&lt;$this-&gt;num_chars; $index++){
                $rand_color = imagecolorallocate($this-&gt;image,rand(0,255),rand(0,255),rand(0,255));
                imagechar($this-&gt;image,3,$x*$index+2,$y-$y/2,$this-&gt;createChar(),$rand_color);
            }
        }

        //out pic
        private function outPic(){
            header("content-type:image/png");
            imagepng($this-&gt;image);
        }
        

         function __destruct(){
             imagedestroy($this-&gt;image);
         }
    }
    
    /*$code = new ValidationCode();
    //echo $code-&gt;createChar();
    $code-&gt;showImage();*/
?&gt;</code>
Copy after login
Copy after login

code.php

<code>&lt;?php
     require_once("validate.php");
    $code = new ValidationCode();
    //echo $code-&gt;createChar();
    $code-&gt;showImage();
  ?&gt;</code>
Copy after login
Copy after login

This question has been closed, reason: I have found the cause of the problem myself

Reply content:

File name: Originally ValidationCode.php
After changing to validate.php, the verification code can be displayed normally.
The browser requests the code.php file to display the verification code

<code>&lt;?php
    class ValidationCode{
        private $width;
        private $height;
        private $num_chars;
        private $image;
        const BORDER = 1;

        function __construct($width=60,$height=20,$num_chars=4){
            $this-&gt;width = $width;
            $this-&gt;height = $height;
            $this-&gt;num_chars = $num_chars;
        }
        
        function showImage(){
            $this-&gt;createImage();
            $this-&gt;drawBorder();
            $this-&gt;drawChars();
            $this-&gt;outPic();
        }
        
        //create canvas
         function createImage(){
            $this-&gt;image = imagecreate($this-&gt;width,$this-&gt;height);
            //$rand_color = imagecolorallocate($this-&gt;image,rand(0,255),rand(0,255),rand(0,255));
            $back = imagecolorallocate($this-&gt;image,0,0,0);
            $border = imagecolorallocate($this-&gt;image,255,255,255);
            imagefill($this-&gt;image,0,0,$back);
            //imagefilledrectangle($thid-&gt;image,self::BORDER,self::BORDER,$this-&gt;width-self::BORDER,$this-&gt;height-self::BORDER,$border);
            //imageline($this-&gt;image,1,1,100,100,$rand_color);
            //$this-&gt;outPic();
        }

        //draw border
        private function drawBorder(){
            $outer_bg_color = imagecolorallocate($this-&gt;image,0,0,0);
            $inner_bg_color = imagecolorallocate($this-&gt;image,255,255,255);
            imagefill($this-&gt;image,0,0,$outer_bg_color);
            imagefilledrectangle($this-&gt;image,self::BORDER,self::BORDER,$this-&gt;width-self::BORDER-1,$this-&gt;height-self::BORDER-1,$inner_bg_color);

        }        
    
        //create char content
          function createChar(){
            $rand_ascii="";
            $rand_type = rand(0,2);
            switch($rand_type){
                case 0:
                    $rand_ascii = rand(48,57);  
                   break;
                case 1:
                     $rand_ascii = rand(65,90);
                   break;
                case 2:
                     $rand_ascii = rand(97,122);
                   break;
            }
            $rand_str = sprintf("%c",$rand_ascii);
            return $rand_str;
        }
    
    
    
        //draw char
         private function drawChars(){
            $x = $this-&gt;width/$this-&gt;num_chars+1;
            $y = $this-&gt;height/2;
            for($index = 0; $index&lt;$this-&gt;num_chars; $index++){
                $rand_color = imagecolorallocate($this-&gt;image,rand(0,255),rand(0,255),rand(0,255));
                imagechar($this-&gt;image,3,$x*$index+2,$y-$y/2,$this-&gt;createChar(),$rand_color);
            }
        }

        //out pic
        private function outPic(){
            header("content-type:image/png");
            imagepng($this-&gt;image);
        }
        

         function __destruct(){
             imagedestroy($this-&gt;image);
         }
    }
    
    /*$code = new ValidationCode();
    //echo $code-&gt;createChar();
    $code-&gt;showImage();*/
?&gt;</code>
Copy after login
Copy after login

code.php

<code>&lt;?php
     require_once("validate.php");
    $code = new ValidationCode();
    //echo $code-&gt;createChar();
    $code-&gt;showImage();
  ?&gt;</code>
Copy after login
Copy after login

Test: Entry Code

<code>&lt;?php
require_once("ValidationCode.php");//include_once("ValidationCode.php");
$code = new ValidationCode();
//echo $code-&gt;createChar();
$code-&gt;showImage();</code>
Copy after login

File name: Include does not work when the php file name and class name are the same?

Test results:
Include does not work when the php file name and class name are the same?

Conclusion: The file name has nothing to do with the class name when including. The result is OK. I don't know why yours can't. But I tested it and it's OK.

It’s really fake! References to file names and class names do not work, so magical! Are there any error messages

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles