larutan
Autoload kelas dan fungsi dari file dan direktori yang berbeda [Autoload classes and functions from different files and directories]
function __autoload[$class_name]
{
//class directories
$directorys = array[
'./Controls/',
'./Config/',
'./Utility/'
];
//for each directory
foreach[$directorys as $directory]
{
//see if the file exsists
if[file_exists[$directory.$class_name . '.php']]
{
require_once[$directory.$class_name . '.php'];
//only require the class once, so quit after to save effort [if you got more, then name them something else
return;
}
}
}
Saya telah memperbarui kode dan itu termasuk file, tetapi satu‑satunya masalah yang saya miliki adalah fungsi ini tidak berjalan secara otomatis,
misalnya file autoload saya ada di : root/ Controls/autoload.php
dan saya memerlukan beberapa kelas dan fungsi di :root/portal/index.php
ketika saya mendefinisikan kelas di index.php saya mendapatkan
kesalahan bahwa file tersebut tidak ada dan saya harus memanggil file autoload.php
secara manual di index.php
bagaimana saya bisa membuat autoload pintar yang seharusnya' t telah memasukkannya ke dalam setiap file untuk memasukkan kelas?
tolong bantu saya. terima kasih sebelumnya
tetapi satu‑satunya masalah yang saya miliki adalah fungsi ini tidak berjalan secara otomatis,misalnya file autoload saya ada di: root/Controls/autoload.php
dan saya memerlukan beberapa kelas dan berfungsi di :root/portal/index.php
ketika saya mendefinisikan kelas di index.php saya mendapatkan kesalahan bahwa file tersebut tidak ada dan saya harus memanggil secara manual autoload.php
file di index.php
bagaimana saya bisa membuat autoload pintar yang seharusnya tidak saya sertakan di setiap file untuk memasukkan kelas?
tolong bantu saya. terima kasih sebelumnya
tetapi satu‑satunya masalah yang saya miliki adalah fungsi ini tidak berjalan secara otomatis,misalnya file autoload saya ada di: root/Controls/autoload.php
dan saya
memerlukan beberapa kelas dan berfungsi di :root/portal/index.php
ketika saya mendefinisikan kelas di index.php saya mendapatkan kesalahan bahwa file tersebut tidak ada dan saya harus memanggil secara manual autoload.php
file di index.php
bagaimana saya bisa membuat autoload pintar yang seharusnya tidak saya sertakan di setiap file untuk memasukkan kelas?
tolong bantu saya. terima kasih sebelumnya
dan saya memerlukan beberapa kelas dan fungsi di :root/portal/index.php
ketika saya mendefinisikan kelas di index.php
saya mendapatkan kesalahan bahwa file tersebut tidak ada dan saya harus panggil file autoload.php
secara manual di index.php
bagaimana saya bisa membuat autoload pintar yang seharusnya tidak saya sertakan di setiap file untuk disertakan kelas?
tolong bantu saya. terima kasih sebelumnya
dan saya memerlukan beberapa kelas dan fungsi di :root/portal/index.php
ketika saya mendefinisikan kelas di index.php saya mendapatkan kesalahan bahwa file tersebut tidak ada dan saya harus panggil file autoload.php
secara manual di index.php
bagaimana saya bisa membuat autoload pintar yang seharusnya tidak saya sertakan di setiap file untuk disertakan kelas?
tolong bantu saya. terima kasih sebelumnya
t telah memasukkannya ke dalam setiap file untuk memasukkan kelas?tolong bantu saya. terima kasih sebelumnya
t telah memasukkannya ke dalam setiap file untuk memasukkan kelas?tolong bantu saya. terima kasih sebelumnya
larutan
larutan 1:
Your program is only as smart as you. If you dream of solving a problem beyond a mainstream language's capability, problem is with your dream, not the language.
To autoload PHP code, set
auto_prepend_file
in php.ini to your autoloader script. Alternatively you may make it the centralised entry point and direct traffic with mod_rewirte.
This is bad practice. Both methods relies on server config such as .htaccess, and is not always available. You may also override existing configs or fail to rewrite some paths.
You should manually centralise the PHP requests [and seal off non‑entry points], or require_once in each entry points.
- PHP does not support autoloading function. Include all functions you might need if you can't bother to code their inclusions.
Look at Wikipedia, Magento, or WordPress. They have hundreds if not thousands global functions.
Do not worry about including ‑ trust your disk cache and enable opcode cache. Static inclusion is pretty fast and with opcode cache it may be faster than conditional loads.
- If you have multiple classes / functions in a file, I know of no dynamic language that can magically pick the correct file to include. Your autoloader looks pretty standard.
You can dynamically define anything in PHP. PHP can never be confident what it will get unless it really runs the code. Auto loaders are simply trusting filenames as correct and complete declaration of what is in the file.
Since your autoloader scans all folders, you may want to make a static file map on first run, and use the map for subsequence loads. Assuming you are not going to create class files dynamically with PHP, of course.
larutan 2:
Simple manual solution : put your autoload
file in your project root and include it in your index file this will do the job.
but if you want to use htaccess
or php.ini
: Place a file called .user.ini
into the document root and add the auto_prepend_file
directive in there:
auto_prepend_file = /home/user/domain.com/init.php
The file must be inside PHP's include_path. So you must either set the file's directory to be in the include_path inside php.ini, or do it in the .htaccess with a php_value statement.
php_value include_path ".:/path/to/file_directory"
php_value auto_prepend_file "file.php
If you use the above method in .htaccess, be sure to copy the include_path from php.ini in and add the :/path_to/file_directory
so you don't lose any already needed includes.
Alternatively, just add :/path/to/file_directory to include_path directly in the php.ini
Update
If you cannot modify the include_path, you might try specifying a relative path to the auto_prepend_file. This should work since the file path sent is processed identically as if it was called with require[]:
php_value auto_prepend_file "./file.php"
[by anonymox、Sheepy、Mohammad_Hosseini]
larutan
- Autoload classes and functions from different files and directories [CC BY‑SA 2.5/3.0/4.0]