URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 1
PHP
1 3,000 Web
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 2

•

• ( )

1.
2.
3.
4. PHP
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 3
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 4

•
•

•
•

•
•
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 5


関数
関数
関数 データ
関数
データ
データ
関数
クラスA クラスB
関数
関数
データ データ
データ 関数
関数
関数
DB処理 メール処理
役
割
分
担
が
明
確
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 6

• 1960 Simula

•
•


• ( )

•

objectA objectB
objectC
objectD
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 7

• class
 PHP class
• (class) (function)
 class
–
 (class)
•
•
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 8
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 9


• $this
• public

<?php
class Person // 人を表すクラス
{
public $name = ''; // 名前を記憶するプロパティ
public function sayHello() // あいさつするメソッド
{
echo 'こんにちは、' . $this->name . 'です。';
}
}
Person.php
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 10


•
 new
– $object = new ();
•
 $object-> = 'foo'
 echo $object->
•
 $object-> ()
<?php
require_once('Person.php');
$taro = new Person(); // newでオブジェクトを作成します
$taro->name = 'アシアル太郎'; // name属性の値を変更します
$taro->sayHello(); // 「こんにちは、アシアル太郎です。」と出力されます
index.php
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 11

•

•


URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 12

• public private protected 3
 public
• $taro = new Person();
• $taro->name = ' '; // public OK
 private
• $taro = new Person();
• $taro->name = ' '; // private
 $taro->setName(' '); // public OK
 protected
• private
•
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 13
private
 Person.php

• $name private
• $name setName()
<?php
class Person
{
private $name = '';
public function setName($name)
{
$this->name = trim($name);
}
public function sayHello()
{
echo 'こんにちは、' . $this->name . 'です。';
}
}
Person.php
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 14
 index.php
<?php
require_once('Person.php');
$taro = new Person();
$taro->setName('アシアル太郎');
$taro->sayHello();
index.php
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 15

•

•


URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 16

• private
• private
 private
•
 public
•
 private
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 17

•
• __construct
 construct
•

• __destruct()
•
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 18
 Person.php
<?php
class Person // アクセス修飾子の例
{
private $name = '';
public function __construct($name)
{
$this->setName($name);
}
public function __destruct()
{
echo 'さようなら';
}
public function setName($name)
{
$this->name = trim($name);
}
public function sayHello() { // あいさつするメソッド (publicなのでどこからでも呼べる)
echo 'こんにちは、' . $this->name . 'です。';
}
}
Person.php
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 19
 index.php
<?php
require_once('Person.php');
$taro = new Person('アシアル太郎');
$hanako = new Person('アシアル花子');
$taro->sayHello();
$hanako->sayHello();
index.php
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 20


• User
• Item
• Mail

• : read -> CsvFileReader
• : control -> OrderController
• : manage -> ConnectionManager
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 21

• save()
• getParameter($name)
• findByUserName($userName)
 boolean (true/false) is has
• isAvailable()
• isDisabled()
• hasParameter($name)

• Item isAvailable()
• Database connect()
• User isLoggedIn()
URL : http://www.asial.co.jp/ Copyright © Asial Corporation. All Rights Reserved. 22
SimpleXML
 SimpleXML
• XML
• RSS
• http://php.net/manual/ja/book.simplexml.php
 RSS
• dump
<?php
$rss = simplexml_load_file('http://blog.asial.co.jp/rss/rss2.0.php');
// var_dump($rss);
// var_dump(get_class_methods($rss));
foreach ($rss->channel->item as $item) {
echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8')."<br>¥n";
// var_dump($item->guid->attributes());
}

【アシアル塾】PHPオブジェクト指向再入門・第一回クラスとオブジェクト

  • 1.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 1 PHP 1 3,000 Web
  • 2.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 2  •  • ( )  1. 2. 3. 4. PHP
  • 3.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 3
  • 4.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 4  • •  • •  • •
  • 5.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 5   関数 関数 関数 データ 関数 データ データ 関数 クラスA クラスB 関数 関数 データ データ データ 関数 関数 関数 DB処理 メール処理 役 割 分 担 が 明 確
  • 6.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 6  • 1960 Simula  • •   • ( )  •  objectA objectB objectC objectD
  • 7.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 7  • class  PHP class • (class) (function)  class –  (class) • •
  • 8.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 8
  • 9.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 9   • $this • public  <?php class Person // 人を表すクラス { public $name = ''; // 名前を記憶するプロパティ public function sayHello() // あいさつするメソッド { echo 'こんにちは、' . $this->name . 'です。'; } } Person.php
  • 10.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 10   •  new – $object = new (); •  $object-> = 'foo'  echo $object-> •  $object-> () <?php require_once('Person.php'); $taro = new Person(); // newでオブジェクトを作成します $taro->name = 'アシアル太郎'; // name属性の値を変更します $taro->sayHello(); // 「こんにちは、アシアル太郎です。」と出力されます index.php
  • 11.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 11  •  •  
  • 12.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 12  • public private protected 3  public • $taro = new Person(); • $taro->name = ' '; // public OK  private • $taro = new Person(); • $taro->name = ' '; // private  $taro->setName(' '); // public OK  protected • private •
  • 13.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 13 private  Person.php  • $name private • $name setName() <?php class Person { private $name = ''; public function setName($name) { $this->name = trim($name); } public function sayHello() { echo 'こんにちは、' . $this->name . 'です。'; } } Person.php
  • 14.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 14  index.php <?php require_once('Person.php'); $taro = new Person(); $taro->setName('アシアル太郎'); $taro->sayHello(); index.php
  • 15.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 15  •  •  
  • 16.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 16  • private • private  private •  public •  private
  • 17.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 17  • • __construct  construct •  • __destruct() •
  • 18.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 18  Person.php <?php class Person // アクセス修飾子の例 { private $name = ''; public function __construct($name) { $this->setName($name); } public function __destruct() { echo 'さようなら'; } public function setName($name) { $this->name = trim($name); } public function sayHello() { // あいさつするメソッド (publicなのでどこからでも呼べる) echo 'こんにちは、' . $this->name . 'です。'; } } Person.php
  • 19.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 19  index.php <?php require_once('Person.php'); $taro = new Person('アシアル太郎'); $hanako = new Person('アシアル花子'); $taro->sayHello(); $hanako->sayHello(); index.php
  • 20.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 20   • User • Item • Mail  • : read -> CsvFileReader • : control -> OrderController • : manage -> ConnectionManager
  • 21.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 21  • save() • getParameter($name) • findByUserName($userName)  boolean (true/false) is has • isAvailable() • isDisabled() • hasParameter($name)  • Item isAvailable() • Database connect() • User isLoggedIn()
  • 22.
    URL : http://www.asial.co.jp/Copyright © Asial Corporation. All Rights Reserved. 22 SimpleXML  SimpleXML • XML • RSS • http://php.net/manual/ja/book.simplexml.php  RSS • dump <?php $rss = simplexml_load_file('http://blog.asial.co.jp/rss/rss2.0.php'); // var_dump($rss); // var_dump(get_class_methods($rss)); foreach ($rss->channel->item as $item) { echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8')."<br>¥n"; // var_dump($item->guid->attributes()); }