I know that php can't nest classes, but it seems that it can if the two classes are in two files:
MainClass.php:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
class mainclass
{
private $var;
public function __construct()
{
require_once ('subclass.php');
$subinstant = new subclass();
}
}
$maininstant = new mainclass();
And subclass.php
<?php
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
class subclass
{
public function __construct()
{
$this->var="this-var
";
echo $this->var;
$test="test in construct
";
echo $test;
function testvar()
{
//$this->var = "this-var in fun
";
//echo $this->var;
$funtest="test in fun
";
echo $funtest;
}
testvar();
}
}
No errors output, and the result is as expected. I just don't understand the require_once
, it seems that it will include the code in subclass.php
, so what's the difference, compared to write it directly at the same position?
What's more, how can I access the variable $var
in testvar()
? (Use as many methods as you can think), thanks!