1. Go to this page and download the library: Download tekintian/pinyin_utils library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tekintian / pinyin_utils example snippets
// 载入自动加载: 如果使用框架的话这个步骤可以忽略。
vert("云南网");
echo $py;
~~~
## sources
## How to autoload the PSR-4 way?
PSR-4 is the newest standard of autoloading in PHP, and it compels us to use namespaces.
We need to take the following steps in order to autoload our classes with PSR-4:
a. Put the classes that we want to autoload in a dedicated directory. For example, it is customary to convene the classes that we write into a directory called src/, thus, creating the following folder structure:
~~~html
your-website/
src/
Db.php
Page.php
User.php
~~~
b. Give the classes a namespace. We must give all the classes in the src/ directory the same namespace. For example, let's give the namespce Acme to the classes. This is what the Page class is going to look like:
~~~php
namespace Acme;
class Page {
public function __construct()
{
echo "hello, i am a page.";
}
use Acme\Db;
use Acme\User;
use Acme\Page;
$page1 = new Page();
~~~
How to autoload if the directory structure is complex?
Up till now, we demonstrated autoloading of classes that are found directly underneath the src/ folder, but how can we autoload a class that is found in a subdirectory? For example, we may want to move the Page class into the Pages directory and, thus, create the following directory tree:
~~~html
your-website/
src/
Db.php
User.php
Pages/
Page.php
~~~
These are the steps that we need to follow:
a. Redefine the namespace. We need to give the Page class a namespace in accordance with its new location in the src/Pages directory.
~~~php
namespace Acme\Pages;
public class Page {
function __construct()
{
echo "hello, i am a page.";
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.