How does OOP manage to 'include' classes s

2019-06-16 10:29发布

I'm trying to move into OOP development and am turning to here as I'm sick of searching the web and finding only the very basic information about what classes are and how they can inherit from each other - that I understand.

What I don't understand as yet is how all these classes can be stored in different files, doted around a folder structure, and yet all you need to do to use the class is just mention its name in your code. How does this work exactly?

And on a related note, if anyone can recommend a good book or online tutorial which will provide a good foundation in OOP (preferably php based) I'd be very grateful.

9条回答
霸刀☆藐视天下
2楼-- · 2019-06-16 10:49

The technology answer is PHP Autoloading.

The implementation answer is this:

One common method used has to do with the names of classes relating to folder structure. There are articles out there, but here is a brief summary:

When setting up your autoload code, take the class name and replace underscores with slashes. This way you can organize your classes in folders.

For example:

Classname: Database_Provider_MySQL

File: Database/Provider/MySQL.php

So in autoload, you'd take the incoming classname, replace the underscores with slashes. Then include that specific file.

This achieves what you are trying to accomplish, you can simply load a class by creating a new instance of it. You never have to use the include statement for these classes.

Do remember to not go to deep where you end up with 6+ levels. I think between 3 and 5 is a good maximum.

Also, this does require that you keep only 1 class per file (similar to Java). Though it might seem inconvenient, it makes locating code a lot easier.

查看更多
Rolldiameter
3楼-- · 2019-06-16 10:50

And on a related note, if anyone can recommend a good book or online tutorial which will provide a good foundation in OOP (preferably php based) I'd be very grateful.

Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development

查看更多
小情绪 Triste *
4楼-- · 2019-06-16 10:50

http://www.headfirstlabs.com/books/hfooad/

Head First Object Oriented Analysis and Design. Head First is a great book series with simple yet shockingly helpful examples. Their whole way of presenting you the information is great. They might even have some some object oriented php books aswell!

查看更多
混吃等死
5楼-- · 2019-06-16 10:57

PHP has functionality to automatically load files based on some call you make. PHP: Autoloading Objects is your definitive resources on the matter.

查看更多
小情绪 Triste *
6楼-- · 2019-06-16 10:57

PHP's include_path configuration setting tells PHP what directories to look in for files.

If you do something like this:

<?php  include 'MyClass.php'; ?>

Then PHP searches the current directory for 'MyClass.php' and then searches any directories on the include_path if it can't find the file. Many class libraries do something like this:

<?php

// Set the ini page to the base package directory
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . dirname(__FILE__));

// Now, in all of your sub-directories, you can still do things like this: 
include 'Subdirectory/Anothersub/AnotherClass.php';

// Now use the class
$obj = new AnotherClass();

?>

It can be further simplified with auto-loading: http://us.php.net/autoload

Other libraries just include every file the use right off the bat in the base package file, so all those classes are available immediately.

查看更多
爷的心禁止访问
7楼-- · 2019-06-16 10:57

Different languages/frameworks do it different ways, but it essential boils down to there's a built-in list of default locations to look and/or you can specify where to look (how you do this again depends on language/framework). I know the question is tagged as php but the question doesn't specifically mention php so thought I'd give a slightly more general answer

查看更多
登录 后发表回答