Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6830439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:37:04+00:00 2026-05-26T22:37:04+00:00

The PHP manual for keyword extends says: Classes must be defined before they are

  • 0

The PHP manual for keyword extends says:

Classes must be defined before they are used!

This code executes without error:

class A extends B {}
class B {}
new A();

But this code returns a fatal error – Class ‘B’ not found in…

class A extends B {}
class B extends A {}
new A();

I had assumed a mutual extension (second snippet) would go into recursion by trying to create A, then B, then A, etc.

But, obviously, by the time it tries to create class B, it realizes that A cannot be extended.

Does anyone know the underlying reason for this behavior? Does PHP keep a record of the classes that it attempts to create and know not to try a second time?


Update:

While this snippets works:

class A extends B {}
class B {}
new A();

This one does not:

class A extends B {} 
class B extends C {}
class C {}
new A();

So, it seems as @Mike pointed out, you must declare the parent class before extending it. (Although PHP is apparently forgiving in the case of a single child class.) That’s why mutual recursion throws the fatal Class ‘B’ not found error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:37:05+00:00Added an answer on May 26, 2026 at 10:37 pm

    Basically yes, a class with a certain name (ignoring namespaces) can only be defined once during script execution. You are essentially trying to define classes multiple (infinite) times.

    Is there a reason you would want to do this? It seems pretty illogical to me. Unless you intended to write constructors to prevent it, all I can see is that you’re trying to make an infinite loop of extension … what’s the point? Curiosity? Don’t you remember what happened to the cat?

    Edit: To quote the PHP manual, “Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first … the order in which the classes are defined is important.” Class B was not found because it had not been declared yet before it was specified as a parent class or A. Reverse the order of your definitions and you’re in the same boat because it’s recursive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the PHP manual , in order to make code more portable, they
I've not used strtok before, the PHP manual doesn't give a very good explanation.
PHP manual suggests to autoload classes like function __autoload($class_name){ require_once(some_dir/.$class_name..php); } and this approach
I was reading the PHP manual for mysqli_stmt_bind_result and saw this code in the
The PHP manual for split() says This function has been DEPRECATED as of PHP
In PHP Manual, there is a note: Note: If this function is not used
In the PHP manual, to show the syntax for functions with optional parameters, they
According to the PHP manual , a class like this: abstract class Example {}
I have the following piece of code taken from the PHP manual on the
What does the following code do? A link to something in the PHP manual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.