0

When it comes to "access class before definition": Why is php treating a standalone class differently from a class implementing an interface? I thought the following code should work, but it doesn't. (Tried with php 7.3.9 and 8.1.11, with or without namespaces...) I know that the order of interface and class definitions is important in case of "extends" and "implements", but this is a different issue here.

Is this a php 'bug' / inconsistency, or a specific language rule I oversighted?

<?php

// This works fine:
$a1 = new A();
// The following line causes error:
$b1 = new B();
// Fatal error: Uncaught Error: Class "B" not found in D:\Coding\PHP\strange.php:6

class A {}

interface I {}

class B implements I {}

// After the class definitions everything is fine (after commenting out line #6 of course)
$a2 = new A();
$b2 = new B();

Note: this old thread is somewhat similar, but not quite the same.

0

1 Answer 1

0

PHP is a sequential language in principle meaning that everything is getting executed in the order the developer write things (when you require/include files, you are actually prepending code). The fact that PHP processes functions and classes before the actual code execution is something done for speeding up, checking things out before proceeding, preparing function symbols and probably adding some high-level language convenience features against older programming languages but not for allowing developers always being able to disregard the sequential order.

PHP actually creates function declarations behind the scenes while parsing the file the way you can manually do it in C/C++ (procedural programming), allowing for calls before definitions. However, class inheritance, interface implementation and conditional definitions are performed in runtime, thus leaving such classes out of the early declaration process. This happens because many things have to be checked eg: a class implementing actually an interface and following the specifications defined in the latter which is subject to execution. So you can't call a function or class which is neither declared nor defined before because there is no other way left for the PHP interpreter to be aware of its existence.

In my opinion, it would be best to maintain a well-defined hierarchy of your code files, the folder structure and the definitions and/or utilize autoloading where possible to avoid such strange conditions.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the insight! Makes sense... I just find it inconsistent that $a1 = new A(); works in my example. Regarding separate file per class: I fully agree with you on that. The issue came up for me in the context of puzzles on www.codingame.com, where the full solution must be in a single file.
You are welcome! Although it is indeed inconsistent for a dev's perspective, class A is internally and automatically declared before you instantiate it while Β cannot be and it is intentionally skipped by the parser for the reason that I've explained. That's why you get the 'Class "B" not found' error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.