2

I have written two class at two different file, for example file classA.php and classB.php, the file written below.

file classA.php

 class a {
      public function __construct() {
           require_once('classB.php');
           $classB = new b();
      }
 }
 $classA = new a();

file classB.php

 class b {
      public function __construct() {
          echo "Hello World!";
      }
 }

And the code work fine, and output Hello World! as well. But when I write both of the class like this

 class a {
     public function __construct() {
         class b {
             public function __construct() {
                 echo "Hello World!";
             }
         }

         $classB = new b();
     }
 }
 $classA = new a();

It showing an error

Fatal error: Class declarations may not be nested

Which i think both of them should be do the same thing, yesterday i found something about "arbitrary places" but i don't find the correct answer for what it is.

I just want to know why both of them, showing different result? or maybe something about include or require that i didn't know?

NOTE

I'm not asking what the different between require() and include() functions

miken32
  • 42,008
  • 16
  • 111
  • 154
Irvan Hilmi
  • 378
  • 1
  • 4
  • 16
  • Possible duplicate of [Difference between require, include, require\_once and include\_once?](https://stackoverflow.com/questions/2418473/difference-between-require-include-require-once-and-include-once) – Zain Farooq Dec 27 '18 at 05:28
  • i mean the different between (included or required classes) and (raw written classes) – Irvan Hilmi Dec 27 '18 at 05:30

3 Answers3

3

Your answer is in the PHP documentation:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

So, unlike for variables, it doesn't matter where the require_once line is: the class will always be at the global scope.

Note that even the namespace of the including file is not applied to the included file, so the included functions and classes are not just in the global scope but also the root namespace.

miken32
  • 42,008
  • 16
  • 111
  • 154
1

You are not allowed to define a class within the definition of another class (unless using the new anonymous class features I'm guessing).

You can define multiple classes within the same file but not one class within the code of another class.

If using Composer to generate classmap based autoloading it will correctly work with multiple classes defined in 1 file.

thinsoldier
  • 151
  • 2
  • 8
  • you mean, using `spl_autoload("className")` right? i just want to know the reason, why the fatal-error said `class declarations may not be nested`. – Irvan Hilmi Dec 27 '18 at 05:38
  • 2
    why the fatal-error said class declarations may not be nested.... Because in your code example you are nesting the definition of class b inside the definition of class a. – thinsoldier Dec 27 '18 at 05:45
  • isn't using `require()` or `include()` equal to defining the class b inside the definition of class a too? – Irvan Hilmi Dec 27 '18 at 05:48
-1
class a {
    public function __construct() {
         require_once('classB.php');
         $classB = new b();
    }
}
$classA = new a();

Is not same as

class a {
    public function __construct() {
        class b {
            public function __construct() {
                echo "Hello World!";
            }
        }

        $classB = new b();
    }
}

This willl also work:

require_once('classB.php');
class a {
    public function __construct() {         
         $classB = new b();
    }
}
$classA = new a();

if you use include() or require(). That means You can use their properties like variable,methos,etc.

Example: You have vars.php file

<?php
$color='red';
$car='BMW';
?>

THen You can use $color and $car in html like this :

<h1>Welcome to my home page!</h1>
  <?php include 'vars.php';
     echo "I have a $color $car.";
  ?>

class is something like properties. You can use by creating instance of that. You will get more idea from source

Dhruv Raval
  • 1,535
  • 1
  • 8
  • 15
  • i know, the code that you suggest is working totally fine, i have tried that earlier, but that wasn't answer my question – Irvan Hilmi Dec 27 '18 at 05:50
  • i don't understand, if it was only variable, i can declared it freely inside class a definition, the question is why the class didn't :v – Irvan Hilmi Dec 27 '18 at 05:59