Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions asial-oop/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

class Application
{
private $logger;
private $logger;

public function setLogger(FileLogger $logger)
{
$this->logger = $logger;
}
public function setLogger(FileLogger $logger)
{
$this->logger = $logger;
}

public function log($message)
{
$this->logger->log($message);
}
public function log($message)
{
$this->logger->log($message);
}
}

61 changes: 27 additions & 34 deletions asial-oop/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,31 @@

class Employee extends Person
{

private $base_salary = 200000;
private $annual_pay_rise = 10000;
private $service_years = 0;

public function __construct($name, $service_years)
{
$this->setName($name);
// parent::__construct($name); // 親のコンストラクタを呼ぶ
$this->setServiceYears($service_years);
}

public function setServiceYears($service_years)
{
$this->service_years = $service_years;
}

public function getSalary()
{
$salary = $this->base_salary + ($this->annual_pay_rise * $this->service_years);
return $salary;
}

public function displaySalary()
{
echo $this->getName() . 'さんの給与は' . number_format($this->getSalary()) . '円です';
}

private $base_salary = 200000;
private $annual_pay_rise = 10000;
private $service_years = 0;

public function __construct($name, $service_years)
{
$this->setName($name);
// parent::__construct($name); // 親のコンストラクタを呼ぶ
$this->setServiceYears($service_years);
}

public function setServiceYears($service_years)
{
$this->service_years = $service_years;
}

public function getSalary()
{
$salary = $this->base_salary + ($this->annual_pay_rise * $this->service_years);

return $salary;
}

public function displaySalary()
{
echo $this->getName().'さんの給与は'.number_format($this->getSalary()).'円です';
}
}






13 changes: 5 additions & 8 deletions asial-oop/Exception.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php

// エラー出力する場合
ini_set( 'display_errors', 1 );
ini_set('display_errors', 1);

try {
echo 'try開始';
throw new Exception('はじめてのException');
echo 'try終了';
echo 'try開始';
throw new Exception('はじめてのException');
echo 'try終了';
} catch (Exception $e) {
echo $e->getMessage();
echo $e->getMessage();
}



33 changes: 16 additions & 17 deletions asial-oop/FileLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

class FileLogger
{
private $path = '';
private $path = '';

public function __construct($path)
{
$this->setPath($path);
}
public function __construct($path)
{
$this->setPath($path);
}

public function setPath($path)
{
$this->path = $path;
}
public function setPath($path)
{
$this->path = $path;
}

public function log($message)
{
file_put_contents($this->path, $message . PHP_EOL, FILE_APPEND);
// PHP_EOL # 改行
// FILE_APPEND # 第3引数に記述すると追記モード
}

}
public function log($message)
{
file_put_contents($this->path, $message.PHP_EOL, FILE_APPEND);
// PHP_EOL # 改行
// FILE_APPEND # 第3引数に記述すると追記モード
}
}
20 changes: 10 additions & 10 deletions asial-oop/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

class Mail
{
private $sender;
private $sender;

public function setSender(IMailSender $sender)
{
$this->sender = $sender;
}
public function setSender(IMailSender $sender)
{
$this->sender = $sender;
}

public function send($message)
{
$this->sender->send($message);
}
}
public function send($message)
{
$this->sender->send($message);
}
}
20 changes: 10 additions & 10 deletions asial-oop/MailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

interface IMailSender
{
public function send($message);
public function send($message);
}

class MailSender implements IMailSender
{
public function send($message)
{
echo 'メールを送ります:' . $message;
}
public function send($message)
{
echo 'メールを送ります:'.$message;
}
}

class DebugMailSender implements IMailSender
{
public function send($message)
{
echo 'メールを送りません' . $message;
}
}
public function send($message)
{
echo 'メールを送りません'.$message;
}
}
20 changes: 9 additions & 11 deletions asial-oop/Person.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
<?php

class Person
class Person
{
private $name = '';
private $age = '';

public function __construct($name, $age)
{
$this->setName($name);
$this->setAge($age);
$this->setName($name);
$this->setAge($age);
}

public function __destruct()
{
echo 'さようなら';
echo 'さようなら';
}

public function setName($name)
{
$this->name = trim($name);
$this->name = trim($name);
}

public function getName()
{
return $this->name;
return $this->name;
}

public function setAge($age)
{
$this->age = trim($age);
$this->age = trim($age);
}

public function sayHello()
{
echo 'こんにちは、' . $this->name . 'です。';
echo '年齢は' . $this->age . '歳です。';
echo 'こんにちは、'.$this->name.'です。';
echo '年齢は'.$this->age.'歳です。';

/*
if ($this->age !== '') {
echo '年齢は' . $this->age . '歳です。';
}
*/

}

}
8 changes: 3 additions & 5 deletions asial-oop/application_client.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

// エラー出力する場合
ini_set( 'display_errors', 1 );
ini_set('display_errors', 1);

require_once('FileLogger.php');
require_once('Application.php');
require_once 'FileLogger.php';
require_once 'Application.php';

$logger = new FileLogger('/var/www/html/repos/php-lesson/asial-oop/temp/log.txt');

Expand All @@ -14,5 +14,3 @@
$app->log('アプリケーション起動');
$app->log('処理を受け付けました');
$app->log('アプリケーション終了');


7 changes: 3 additions & 4 deletions asial-oop/employee_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
//ini_set( 'display_errors', 0 );

// エラー出力する場合
ini_set( 'display_errors', 1 );
ini_set('display_errors', 1);


require_once('Person.php');
require_once('Employee.php');
require_once 'Person.php';
require_once 'Employee.php';

$taro = new Employee('アシアル太郎', 20);
$taro->displaySalary();
3 changes: 1 addition & 2 deletions asial-oop/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once('Person.php');
require_once 'Person.php';

$taro = new Person('アシアル太郎', 20);
$hanako = new Person('アシアル花子', 18);
Expand All @@ -12,7 +12,6 @@
unset($taro); // オブジェクトの破棄
// $tato = "hogehoge"; // こっちでも破棄される


$hanako->sayHello();

//echo 'test';
6 changes: 3 additions & 3 deletions asial-oop/strategy_client.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

// エラー出力する場合
ini_set( 'display_errors', 1 );
ini_set('display_errors', 1);

require_once('Mail.php');
require_once('MailSender.php');
require_once 'Mail.php';
require_once 'MailSender.php';

$mail = new Mail();
$mail->setSender(new DebugMailSender());
Expand Down
20 changes: 9 additions & 11 deletions asial/array.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<?php

// 配列テスト
// 添字なしでも定義加納
// 配列テスト
// 添字なしでも定義加納

$uranai[] = "大吉";
$uranai[] = "中吉";
$uranai[] = "吉";
$uranai[] = '大吉';
$uranai[] = '中吉';
$uranai[] = '吉';

var_dump($uranai);
var_dump($uranai);

print "<br><br><pre>";
var_dump($uranai);
print "</pre>";

?>
echo '<br><br><pre>';
var_dump($uranai);
echo '</pre>';
21 changes: 10 additions & 11 deletions asial/break.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

// continue, break サンプル

for ($i = 1; $i <= 200; $i++) {
if ($i % 2 == 0) {
continue;
}
if ($i > 100) {
break;
}
print $i . "<br>";
}
// continue, break サンプル

for ($i = 1; $i <= 200; $i++) {
if ($i % 2 == 0) {
continue;
}
if ($i > 100) {
break;
}
echo $i.'<br>';
}
Loading