SlideShare a Scribd company logo
1 of 8
Download to read offline
~PHPで学ぶ~
オブジェクト指向プログラミング
じゅん@Railsエンジニア(勉強中)
PHPで学ぶオブジェクト指向プログラミング
①オブジェクト指向プログラミングとは?
「オブジェクト指向の考え方を取り入れ、オブジェクトを組み立てるように表現して、
システムを構築する」プログラミング手法です。
オブジェクトの振る舞いに矛盾がないよう、定義する。
②オブジェクトとは?
オブジェクト指向の言葉を生み出した計算機科学者アラン・ケイの言葉を引用。
“すべてはオブジェクトである。”
“どのオブジェクトもクラスのインスタンスであり、クラスもまたオブジェクトである。”
私は、「オブジェクトとは現実世界の全てモノ。クラスとインスタンスはオブジェクトに含まれる。
クラス != インスタンス 」だと理解しました。間違えていたら、ご指摘下さい、、、
③クラスとインスタンスの関係
クラス :設計書
インスタンス:クラスを実体化したもの
※例を右図に示す。
クラス:エンジニア インスタンス:エンジニアのJunさん
実体化
PHPで学ぶオブジェクト指向プログラミング
(1)クラスとインスタンス
・クラス定義:オブジェクトのメソッド(機能)とプロパティ(属性)を定義する。
<?php
class Engineer {
public $name;
public $age;
public function showName() {
echo “エンジニアの{$this->name}です!”, “\n”;
}
public function showAge() {
echo “年齢は {$ this-> age}です!”, “\n”;
}
}
?>
Engineerの名前・得意言語
といった属性を定義する。
文字列の出力等、実行できる処理
をメソッドとして定義する。
・インスタンスの生成と値の設定
$engineer = new Engineer(“ジュン” , 26);
$engineer ->showName() ;
$engineer ->showLanguage() ;
出力 ⇒ エンジニアのジュンです!
年齢は26歳です!
・インスタンスメソッドの実行
ファイル名: Engineer.php
※コンストラクタの記述は割愛
PHPで学ぶオブジェクト指向プログラミング
(2)クラスの継承
既存のクラスを拡張するように自身のクラスを定義することが可能
親クラス子クラス
<?php
require_once(“Engineer.php”);
class WebEngineer extends Engineer {
public function pullRequest() {
echo “{$this->name}がプルリクエストしました!”, “\n”;
}
}
?>
ファイル名:WebEngineer.php
extends
Engineerクラス定義
ファイルの読み込み
Engineerクラスを継承して
Gitクラスを定義
継承によりEngineerクラスの
${name}プロパティが使用可
インスタンスの生成と値の設定
$engineer = new WebEngineer(“ジュン”);
$engineer-> pullRequest();
出力 ⇒ ジュンがプルリクエストしました!
インスタンスメソッドの実行
子クラスは、親クラスから
プロパティとメソッドを継承する
<?php
require_once(“Git.php”);
Class Engineer {
public $name;
public $language;
use Git1, Git2;
・
・
・
}
?>
PHPで学ぶオブジェクト指向プログラミング
(3)トレイト
PHPでは、複数のクラスを継承する多重継承がサポートされていない。
そこで、トレイトの機能を使うことで複数のトレイトからメソッドを引き継ぐことができる。
トレイトの定義
<?php
trait Git1{
function merge(){
echo “プルリクエストをマージしました!”, “\n”;
}
}
trait Git2{
function comment(){
echo “プルリクエストにコメントしました!”, “\n”;
}
}
トレイトの定義
ファイル名:Git.php
トレイトの利用
ファイル名:Engineer.php
トレイトの利用を宣言
複数のトレイトを指定可
インスタンスの生成とメソッドの実行
$engineer = new Engineer();
$engineer -> merge();
$engineer -> comment();
プルリクエストをマージしました!
プルリクエストにコメントしました!
出力
<?php
require_once(“Insterface.php”);
Class Engineer implements Git{
public function pullrequest() {
echo “プルリクエストしました!”, “\n”;
}
public function merge() {
echo “マージしました!”, “\n”;
}
}
?>
PHPで学ぶオブジェクト指向プログラミング
(4)インターフェース
クラスで実装すべきメソッドを規格として定めることができる。
不特定のクラスで、共通のメソッドを定義したい場合等に使用する。
インターフェースの定義
<?php
Interface Git {
function pullrequest() ;
function merge() ;
}
?>
ファイル名:Interface.php
インターフェースの利用
ファイル名:Engineer.php
Gitインターフェースの規格では、
pullrequest()、merge()を実装しな
ければならない Gitインターフェースで
指定されているメソッドを実装
インスタンスの生成とメソッドの実行
$engineer = new Engineer();
$engineer -> conflict();
$engineer -> merge();
プルリクエストしました!
マージしました!
出力
PHPで学ぶオブジェクト指向プログラミング
(5)抽象クラス
抽象メソッドがあるクラスを、抽象クラスという。
抽象クラスを継承した子クラスで、抽象メソッドをオーバーライドして使用できる。
<?php
require_once(“Abstract.php”);
Class Engineer extends Git {
public function maerge() {
echo “マージしました!”, “\n”;
}
}
?>
抽象クラスの定義
<?php
abstract class Git {
abstract function merge();
}
?>
ファイル名:Abstract.php
抽象クラスの利用
ファイル名:Engineer.php
インスタンスの生成とメソッドの実行
$developer1 = new Engineer();
$ developer1-> merge();
マージしました!
出力
抽象メソッドの定義
継承した抽象メソッドの
オーバーライド
PHPで学ぶオブジェクト指向プログラミング
先輩からのアドバイス
・クラスの名前付けは、とても重要。継承関係等を意識したクラス名をつけるべき
・オブジェクトの振る舞いに矛盾がないように定義する
・オブジェクト指向を意識しながら、標準クラス等の優れたコードをリーディングすると効果的
・難しく考えすぎないほうがいい
参考情報
・大重美幸(2018) 『詳細!PHP7 + MySQL 入門ノート』大日本印刷株式会社
・PHPでインターフェイスと抽象クラスを使う
https://qiita.com/nogson/items/e6575d6617f854ed6e25

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

~PHPで学ぶ~ オブジェクト指向プログラミング