-1

PHP developers who are using class features in their projects would have to do a lot of circus to load specific class before using them. php __autoload() (would be deprecated in future), spl_autoload_register(), psr-0 & psr-4 standards to autoload them.

The __autoload() or for that matter spl_autoload_register() function (with paths of all classes in it) defined for example in start.php have to be first included in index or whatever the php page is and then do your work with the loaded class object.

In psr-0 & psr-4 we configure composer.json's by setting autoload's psr-0 or psr-4 or classmap path to vendor folder. Do dump-autload, include that single generated autoload.php in your php file and psr-4 convention of "use" "namespace" said to make our life much easier (which is not).

In java, define classes under packages then simply import required class and use it.

My question is, why is php loading of classes is not as simple as java packages & import?

I know that there might be some implementation difficulties if not nearly impossible.

Few thousands of new c code line if not lakhs would definitely not slow the php interpreter to help find class names which are mentioned in import keyword and load that class from the packages as similar to java, instead of these tedious conventions (spl_autoload_register(),psr-0,psr-4) which are suppose to make loading the classes easier.

I have used java in most of my projects. so, I am new to php and the kind of question might be well expected which might appear pretty noob one. Replies are much appreciated. Thanks.

  • Because you would normally just say `include_once 'otherFile.php';` ? – Rabbit Guy Aug 12 '16 at 18:46
  • 1
    @rabbit, dude, do you know what include_once does seriously? I think, we have well passed that include_once stage in _autload(),spl_autoload_register(), composer.json's autoload file. – Sanjay Kumar Aug 12 '16 at 18:53
  • 2
    Dude, you posted above that you want to know how to do something, you gave zero code, you didn't even post your damn code to show what you were doing, then someone suggested using include_once and you attacked them. And then you sit there wondering why ppl aren't helping you. – Rabbit Guy Aug 12 '16 at 18:57
  • 3
    "Why is PHP [loading classes] not like Java?" Erm, I dunno, perhaps because [peeling] an orange like [peeling] an apple? – VLAZ Aug 12 '16 at 19:17

1 Answers1

1

Well, it is.

PSRs are (currently) only recommendations for framework implementations. That is, unless you're writing a framework or package, it's not really aimed at you. It just so happens that a number of php developers like to follow the standards anyway (particularly PSRs 1, 2, and 12).

As it happens, the PSR-4 examples all use spl_autoload_register(), which is a more convenient way to include packages and classes, without the need of include_once. PSR-4 is only a recommendation by a body separate from the php language, and is relatively recent, which is why you can implement spl_autoload_register() in any way you want.

However, include_once is still a valid method to load a class, and if you ask me can often be the best method, and (not being a Java developer) I assume is just as simple as the equivalent in Java.

thatdamnqa
  • 494
  • 1
  • 4
  • 12
  • haha. thank you. my question was really pretty noob one. I think @rabbit already answered it. But i didn't get it what he has said. Now you made it really clear. sorry rabbit. – Sanjay Kumar Aug 13 '16 at 03:20