3. Операции с файлами
file_exists ($fileName) - проверяет, существует-ли указанный файл.
unlink ($fileName) - удаляет указанный файл.
copy ($source, $dest) - создает копию файла $source и помещает его в файл по имени
$dest.
4. Работа с директориями
$curDir = opendir('.');
while($file = readdir($curDir)) {
echo $file;
}
foreach (glob('*.txt') as $fileName) {
echo $fileName . ' размер ' . filesize($fileName);
}
5. Функции
function sum($x, $y) {
return $x + $y;
}
echo $sum(1, 2);
$string = 'Hello user';
function printHelloWorld() {
$string = 'Hello world';
echo $string;
}
9. Классы и объекты
class BaseObject {
private $greetings = 'Hello world!';
public function sayHello() {
echo $this->greetings;
}
}
$base = new BaseObject();
$base->sayHello();
$base = 'BaseObject';
$base = new $base(); // omg
10. Пространства имен
// 10.php
namespace internshipbase;
class BaseObject {
const GREETINGS = 'Hello world';
public function sayHello() {
echo self::GREETINGS;
}
}
11. // 10_1.php
namespace internshipadvanced;
class BaseObject
{
const GREETINGS = 'Hello intern's';
public function sayHello() {
echo self::GREETINGS;
}
}
// 10.2.php
require '10.php';
require '10_1.php';
$base = new internshipbaseBaseObject();
$advanced = new internshipadvancedBaseObject();
$base->sayHello();
$advanced->sayHello();
12. Наследование
require '10.php';
class AdvancedObject extends internshipbaseBaseObject {
public function sayHi($default = true)
{
if ($default) {
parent::sayHello();
} else {
echo 'Hi peoples';
}
}
}
$adv = new AdvancedObject();
$adv->sayHello();
$adv->sayHi();
13. Модификаторы доступа / область видимости.
class BaseObject
{
public $greetings = 'Hello world';
protected $protectedGreetings = 'Hello my world!';
private $privateGreetings = 'Hello my personal world';
}
$obj = new BaseObject();
echo $obj->greetings;
echo $obj->protectedGreetings; // error
echo $obj->privateGreetings; // error
14. Static
class BaseObject
{
const GREETINGS = 'Hello world';
public static $magicNumber = 10;
public static function sayHello()
{
echo self::GREETINGS;
}
}
BaseObject::sayHello();
echo BaseObject::GREETINGS;
echo BaseObject::$magicNumber;
15. Контроль типов
class BaseObject
{
private $greetings = 'Hello world';
public function getGreetings()
{
return $this->greetings;
}
}
function sayHello(BaseObject $obj)
{
echo $obj->getGreetings();
}
17. Абстрактные классы
abstract class Apple
{
protected $name = 'apple';
abstract public function getColor();
public function getName()
{
return $this->name;
}
}
18. class GreenApple extends Apple
{
private $color = 'green';
public function getColor()
{
return $this->color;
}
public function getName()
{
return $this->getColor() . ' ' . parent::getName();
}
}
19. Исключения
try {
$age = rand(0, 200);
if ($age > 150) {
throw new Exception('Immortal human detected');
}
} catch (Exception $e) {
echo $e->getMessage() . '<br>';
} finally {
echo 'age is: ' . $age;
}
20. Сессии
Способ ассоциации информации с пользователям при нескольких
последовательных запросах. К данным можно получить доступ через массив
$_SESSION.
session_start() - начинает новую или продолжает старую сессию.
session_destroy() - уничтожает все данные в сессии.
21. Паттерн Singleton
class Singleton
{
static private $instance = null;
private function __construct(){}
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
}