Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


derived class defined later in the same file does not exist using -'php,class,include,require,extends'


derived class defined later in the same file does not exist  using -'php,class,include,require,extends'

Let’s suppose we’ve got two php files, a.php and b.php
Here’s content of file a.php:

<?php // content of a.php
class A {
}


And here’s the content of file b.php

<?php  // content of b.php
include dirname(__FILE__) . "/a.php";
echo "A: ", class_exists("A") ? "exists" : "doesn’t exist", "\n";
echo "B: ", class_exists("B") ? "exists" : "doesn’t exist", "\n";
echo "BA (before): ", class_exists("BA") ? "exists" : "doesn’t exist", "\n";
echo "BB: ", class_exists("BB") ? "exists" : "doesn’t exist", "\n";
class B {
}
class BA extends A {
}
class BB extends B {
}
echo "BA (after): ", class_exists("BA") ? "exists" : "doesn’t exist", "\n";


If you launch the b.php script you have this output:

A: exists
B: exists
BA (before): doesn’t exist
BB: exists
BA (after): exists


Why does the BA class exist only after the class definition? And why does the other classes exist even before their definition? Which is the difference? I’d expect to have a common behavior in both cases...
Is there a way I could use the BA class even before its definition?

Thank you

Michele
    

asked Sep 8, 2015 by rajesh
0 votes
10 views



Related Hot Questions



Government Jobs Opening


...