隠蔽(カプセル化)
(c) 2013 MasashiShinbara @shin1x1
• アクセス修飾子
<?php
class Foo
{
public $public = 'public';
protected $protected = 'protected';
private $private = 'private';
}
public どこからでもアクセス可能
protected 定義したクラス、継承したクラス
private 定義したクラスのみ
8.
隠蔽(カプセル化)
(c) 2013 MasashiShinbara @shin1x1
<?php
class Foo
{
public function public_method()
{
// どこからでも実行可
}
protected function protected_method()
{
// Fooクラスとその継承クラス
}
private function private_method()
{
// Fooクラスのみ
}
}
9.
継承
(c) 2013 MasashiShinbara @shin1x1
<?php
class Foo
{
public function hello()
{
echo 'Foo'.PHP_EOL;
}
}
class FooChild extends Foo
{
public function hello()
{
echo 'FooChild'.PHP_EOL;
}
}
$obj = new FooChild();
$obj->hello(); // FooChild
• 単一継承のみ
継承
10.
多態性(ポリモフィズム)
(c) 2013 MasashiShinbara @shin1x1
<?php
interface Printable
{
public function output();
}
class Foo implements Printable
{
public function output()
{
echo 'Foo';
}
}
class Bar implements Printable
{
public function output()
{
echo 'Bar';
}
}
11.
多態性(ポリモフィズム)
(c) 2013 MasashiShinbara @shin1x1
<?php
class SomeObject
{
public function execute(Printable $obj)
{
$obj->output();
}
}
$obj = new SomeObject();
$obj->execute(new Foo()); // Foo
$obj->execute(new Bar()); // Bar
Printable なら ok
12.
クラスメソッド、定数、変数
(c) 2013 MasashiShinbara @shin1x1
<?php
class Foo
{
const CLASS_CONSTATNS = 'aws'; // クラス定数
static public $classValue = 'abc'; // クラス変数
static public function classMethod()
{
// クラスメソッド
}
}
echo FOO::CLASS_CONSTATNS; // aws
echo Foo::$classValue; // abc
Foo::classMethod();
13.
コンストラクタ、デストラクタ
(c) 2013 MasashiShinbara @shin1x1
<?php
class Foo
{
protected $name = null;
public function __construct($name)
{
$this->name = $name;
echo '__construct'.PHP_EOL;
}
public function __destruct()
{
echo '__destruct'.PHP_EOL;
}
}
$obj = new Foo('Jun'); // __construct
// __destruct
コンストラクタ
デストラクタ
14.
抽象クラス、メソッド
(c) 2013 MasashiShinbara @shin1x1
<?php
abstract class AbstractFoo {
public function something()
{
$this->hello();
}
abstract protected function hello();
}
class Foo extends AbstractFoo
{
protected function hello()
{
echo 'Foo';
}
}
$obj = new Foo();
$obj->something(); // Foo
抽象クラス
(インスタンス化不可)
具象クラス
15.
final
(c) 2013 MasashiShinbara @shin1x1
<?php
final class NoInheritance
{
}
class Foo
{
final public function noInheritanceMethod()
{
//
}
}
継承できない
オーバーライドできない
16.
インターフェイス
(c) 2013 MasashiShinbara @shin1x1
<?php
interface Printable
{
public function printValue($value);
}
interface Writable
{
public function writeValue($value);
}
class Foo implements Printable, Writable
{
public function printValue($value)
{
//
}
public function writeValue($value)
{
//
}
}
複数実装可
17.
タイプヒンティング
(c) 2013 MasashiShinbara @shin1x1
<?php
interface Printable {}
interface Writable {}
class Foo
{
public function bar(Printable $printer, Writable $writer)
{
//
}
public function something(array $array)
{
//
}
}
タイプヒンティング
タイプヒンティング
タイプヒンティング
18.
名前空間
(c) 2013 MasashiShinbara @shin1x1
<?php
namespace Vendorlib;
use AwsS3S3Client;
class Foo
{
public function method()
{
throw new Exception();
}
}
名前空間の宣言
名前空間のインポート
グローバル
VendorlibFoo