0

I cannot autoload parent of class in namespace. Without inheriting child class is loaded; but child is not able to autoload parent class.

File structure:

/index.php
/lib/router.php
/lib/ns1/parent1.php
/lib/ns1/child1.php

index.php:

spl_autoload_extensions(".php");
spl_autoload_register();
require '/lib/router.php';

/lib/router.php

$child1 = new ns1\child1();

/lib/ns1/child1.php

namespace ns1;
class child1 extends parent1 {}

/lib/ns1/parent1.php

namespace ns1;
class parent1 { function __construct() {} }

When I remove "extends" part from child1 everything is ok. With "extends" part I have error:

Fatal error: spl_autoload(): Class parent1\child1 could not be loaded in /lib/ns1/child1.php

Is there some way how to do that with built in default spl autoload function? Many thanks for any help.

Otak
  • 11
  • 4
  • `parent` is a [reserved word](http://php.net/manual/en/reserved.classes.php#reserved.classes.special), it may only be an example but if it is it's a bad one – scrowler Nov 22 '16 at 03:27
  • of course it is only example :) names are for better understanding. but i correct it... – Otak Nov 22 '16 at 11:52

1 Answers1

0

I found the solution which works for me in this answer https://stackoverflow.com/a/7987085/5208203

Just add set_include_path() to file index.php:

set_include_path(get_include_path().PATH_SEPARATOR."/lib");
spl_autoload_extensions(".php");
spl_autoload_register();
require '/lib/router.php';

But I'm not sure about performance, may be it is just workaround for another failure I have in my class definitions...

Community
  • 1
  • 1
Otak
  • 11
  • 4