PHP Classes not found

Hello, I’m new to Docker and not sure where I should post this question so please direct me to the appropriate area if necessary.

I’m testing PHP functionality in Docker and having problems with PHP Classes. Quite simply, my programs are not finding classes. My initial reaction was that something was wrong with my code but when I use CLI from Docker Desktop to navigate to the folder containing the class, commands should as “more” or “move” can’t find the class. I can see the that file containing the class is in the folder using “ls” but no other commands seem to work. Files that do not contain classes work normally.

Can anyone offer any insights as to why my PHP Classes can not be found? Details are below.

Thanks,
Mike

Docker version 20.10.12, build e91ed57

Here’s the class file:

<**?php  // testheader.php**

**class testheader {**

**    private $text;**

**     function __construct($text) {**
**         $this->text = $text;**
**    }**

**  function displayText() {**
**      echo $this->text;**
**  }**
**}**

**?>**


Here's the call...

*<?php //callheader.php*

*include_once 'testheader.php';*

*$testheader = new testheader("Hello World");*

*?>*

Here’s here’s what is returned when I attempt to use the class…

*/var/www/html # php callheader.php *

Warning: include_once(testheader.php): failed to open stream: No such file or directory in /var/www/html/callheader.php on line 3

Warning: include_once(): Failed opening ‘testheader.php’ for inclusion (include_path=‘.:/usr/local/lib/php’) in /var/www/html/callheader.php on line 3

Fatal error: Uncaught Error: Class ‘testheader’ not found in /var/www/html/callheader.php:5
Stack trace:
#0 {main}

  • thrown in /var/www/html/callheader.php on line 5*

Here are the contents of the folder after executing “ls”, as you can see the file is there…

/var/www/html # ls

  • testheader.php callheader.php*

Finally, if I attempt to see the contents of the file using “more” I get a file not found message, but of course, the file is really there.

/var/www/html # more testheader.php
more: testheader.php: No such file or directory

Here’s the docker.compose.yml file

version: "3.2"
services:
  php:
    build: './php/'
    networks:
      - backend
    volumes:
      - ./public_html/:/var/www/html/
  apache:
    build: './apache/'
    depends_on:
      - php
      - mysql
    networks:
      - frontend
      - backend
    ports:
      - "8083:80"
    volumes:
      - ./public_html/:/var/www/html/
  mysql:
    image: mysql:5.6.40
    networks:
      - backend
    environment:
      - MYSQL_ROOT_PASSWORD=rootpassword
    volumes:
      - /Users/mike/vscode_projects/database:/var/lib/mysql
networks:
  frontend:
  backend:

Is testheader.php is a symbolic link to an other file?
Could you try listing the files with ls -l instead of just ls so we can see the file permissions and type?

It is also better to use include with an absolute path like this:

include_once __DIR__ . "/testheader.php";

I would also use require_once since you don’t want your script to continue when the file is not found:

require_once __DIR__ . "/testheader.php";

But this is not related to Docker. It just helps you to see the original error without maybe a dozen of other following error messages.

Ákos, thank you for your suggestions and for taking to time to answer my question. I found the problem. I accidentally appended a leading space to the name of the source file containing the class. That is, " testheader.php". Since my code did not include the leading space attempts to call the class failed.

Mike