Стандарты кодирования

Краткое содержание лекции

PSR-0

(index.php) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
spl_autoload_register('autoload_function');
function autoload_function($className)
{
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require $fileName;
}
echo new \Fightmaster\PHPLecture() . \PHP_EOL;
echo new \Fightmaster_PHPLectureUnderscore() . \PHP_EOL;
echo new \Fightmaster\underscore_directory\PHPLecture() . \PHP_EOL;
echo new \Fightmaster\PHP_Lecture() . \PHP_EOL;
(PHPLecture.php) download
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace Fightmaster;

class PHPLecture
{
    public function __toString()
    {
        return __FILE__ . ' => ' . __CLASS__;
    }
}
(PHPLectureUnderscore.php) download
1
2
3
4
5
6
7
8
9
<?php

class Fightmaster_PHPLectureUnderscore
{
    public function __toString()
    {
        return __FILE__ . ' => ' . __CLASS__;
    }
}
(PHPLecture.php) download
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace Fightmaster\underscore_directory;

class PHPLecture
{
    public function __toString()
    {
        return __FILE__ . ' => ' . __CLASS__;
    }
}
(Lecture.php) download
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace Fightmaster;

class PHP_Lecture
{
    public function __toString()
    {
        return __FILE__ . ' => ' . __CLASS__;
    }
}