مثلا اگه ما تو index.php باشیم و تو همین مسیر یه پوشه به اسم app باشه یه توش فایل routes.php باشه، اگه بخوایم این فایل رو تو index.php استفاده کنیم، میگیم:
require ‘app/routes.php’;
یا
require __DIR__ . ‘/app/routes.php’;
حالا اگه بخوایم از use برای ایمپورت کردن استفاده کنیم هم به این فانکشنها نیازداریم.
در واقع PHP به خودی خود namespace ها رو نمیفهمه و در نهایت باید اونو تبدیل کنیم به مسیر فایل و require ش کنیم. بنابراین ما به برنامه PHP میگیم که هر وقت کلاسی رو نتونستی پیدا کنی اینکار رو بکن (تصویر):
در اینجا spl_autoload_register یه چیزیه که تو خود php تعریف شده.
اینو بالای فایل اجرایی تون بنویسید و یا تو یه فایل جدا بریزید و اول از همه ایپورتش (require) کنید.
نکته: از composer که استفاده میکنیم خودش اتومات این کار رو میکنه. نیازی به این کارا نیست.
تبلیغ:
دوره 80 ساعته Laravel+Devops که مباحث مبتدی و پیشرفته لاراول به همراه مخلفاتشو با هم میریم جلو: mysql, amazon s3, docker, xdebug, redis, design patterns و کلی چیز دیگه … (امروز جلسه دومش برگزار شد)
به عنوان هدیه دوره هم میتونید در کارگاه ELK رایگان شرکت کنید.
برای دیدن جزئیات دوره + ثبتنام و اطلاع از پرداخت اقساطی میتونی یه سر به لینک زیر بزنی:
ترجمه:
In php, we use require, require_once and include to import a file, and we have to give the full path of that file as input.
For example, if we are in index.php and there is a folder called app in the same path and there is a file routes.php in it, if we want to use this file in index.php, we say:
require ‘app/routes.php’;
or
require __DIR__ . ‘/app/routes.php’;
Now, if we want to use use to import, we also need these functions.
In fact, PHP does not understand namespaces by itself, and finally we have to convert it to a file path and require it. So we tell the PHP program to do this whenever it can’t find a class (image):
Here spl_autoload_register is something defined in php itself.
Write this above your executable file or put it in a separate file and import it first.
Note: The composer we use does this automatically. There is no need to do this.
Advertisement:
The 80-hour Laravel+Devops course, where we will go forward with the beginner and advanced topics of Laravel along with its complications: mysql, amazon s3, docker, xdebug, redis, design patterns and many other things… (the second session was held today)
You can participate in the free ELK workshop as a course gift.
To see the details of the course + registration and to know about the payment of installments, you can visit the following link:
For example, if we are in index.php and there is a folder called app in the same path and there is a file routes.php in it, if we want to use this file in index.php, we say:
require ‘app/routes.php’;
or
require __DIR__ . ‘/app/routes.php’;
Now, if we want to use use to import, we also need these functions.
In fact, PHP does not understand namespaces by itself, and finally we have to convert it to a file path and require it. So we tell the PHP program to do this whenever it can’t find a class (image):
Here spl_autoload_register is something defined in php itself.
Write this above your executable file or put it in a separate file and import it first.
Note: The composer we use does this automatically. There is no need to do this.
Advertisement:
The 80-hour Laravel+Devops course, where we will go forward with the beginner and advanced topics of Laravel along with its complications: mysql, amazon s3, docker, xdebug, redis, design patterns and many other things… (the second session was held today)
You can participate in the free ELK workshop as a course gift.
To see the details of the course + registration and to know about the payment of installments, you can visit the following link: