技術情報
- 2020年12月11日
- 技術情報
[Laravel] Eloquentとページネーション(4)
自力でページネーションの実装をおこなうには、大変工数がかかりますが、Laravelフレームワークが用意しているページネーションを使用することで工数が大幅に軽減されます。
今回はLaravelフレームワークのページネーションのカスタマイズ方法を紹介したいと思います。
本記事は前回の「[Laravel] Eloquentとページネーション(3)」の続きになります。
nishida at 2020年12月11日 10:00:03
- 2020年12月07日
- 技術情報
Polymorphism in PHP
Polymorphism sounds difficult in terms of its vocabulary but it’s not that complex. Today I will write some explanations along with simple code examples. There are two types of polymorphism called dynamic polymorphism and static polymorphism.
Let’s take a look at the dynamic polymorphism first. It can be achieved using interface.
<?php
interface Country
{
public function talk();
}
class Myanmar implements Country
{
public function talk()
{
return "Mingalarpr";
}
}
class Japan implements Country
{
public function talk()
{
return "ko ni chi wa";
}
}
?>
There is an interface called Country and two of the concrete classes called Myanmar and Japan implemented it. The type of two concrete classes, Myanmar & Japan are same as they are the country but the function called talk inside their classes will be different depending on the country.
$myanmar = new Myanmar();
$japan = new Japan();
echo $myanmar->talk(); //output - Mingalarpr
echo $japan->talk(); //output - ko ni chi wa
As I said earlier before their talk function is different. That is called dynamic polymorphism. Concrete classes are sharing the same interface as they are common in type but different in functionalities.
There is one more polymorphism called static polymorphism. This can be achieved using method overloading. We will use __call magic method as we are writing in PHP.
<?php
class Talk
{
function __call($name,$arg){
switch(count($arg)){
case 1 : return $arg[0];
case 2 : return $arg[0] . '&' .$arg[1];
}
}
}
$talk = new Talk();
echo $talk->lang("Myanmar"); //output - Myanmar
echo $talk->lang("Myanmar","Japan"); //output Myanmar & Japan
There are different return depending on the method arguments. The class talk can be adaptable depending on the function arguments which is called static polymorphism.
You can see the magic method __call reference here.
https://www.php.net/manual/en/language.oop5.overloading.php#object.call
Thanks for reading. By Yuuma.
yuuma at 2020年12月07日 11:00:11
- 2020年11月30日
- 技術情報
Dependency Injection in PHP
Dependency injection is the process by which one object provides the dependencies of another object. Dependency injection is a software design approach that avoids hard-coded dependencies and allows you to change dependencies at runtime and compile time.
Lets take a look along with the examples. Lets say we have a Food class that will supply dependency to Dinner Class.
<?php
class Food
{
protected $food;
public function __construct($food)
{
$this->food = $food;
}
public function get()
{
return $this->food;
}
}
After that, lets create a dinner class without dependency injection first.
<?php
class Dinner
{
public function eat()
{
$food = new Food("Chicken soup");
return $food->get();
}
}
$dinner = new Dinner();
echo $dinner->eat(); //output - Chicken soup
In the above style of Dinner class, we have hardcoded food class that is bad in coupling and not well dependency injected. Lets fix our dinner class using dependency injection.
<?php
class Dinner
{
protected $dinner;
//receiving the dependency object food.
public function __construct(Food $food)
{
$this->dinner = $food;
}
public function eat()
{
return $this->dinner->get();
}
}
$food = new Food("Chicken soup");
//supplying the food object.
$dinner = new Dinner($food);
echo $dinner->eat(); //output - Chicken soup
Now, we have added a constructor to be injected the necessary food dependency object. The code is now loosely coupled and not hardcoded in dinner class anymore.
By Yuuma.
yuuma at 2020年11月30日 11:00:23
- 2020年11月27日
- 技術情報
[Laravel] Eloquentとページネーション(3)
自力でページネーションの実装をおこなうには、大変工数がかかりますが、Laravelフレームワークが用意しているページネーションを使用することで工数が大幅に軽減できます。
今回はLaravelフレームワークのページネーションの実装方法を紹介したいと思います。本記事は前回の「[Laravel] Eloquentとページネーション(2)」の続きになります。
nishida at 2020年11月27日 10:00:32
- 2020年11月16日
- 技術情報
Facade Design Pattern
Facade is an structural design style that provides a simplified interface for a library, framework, or any other complex set of classes.
Let’s say you need to make your code work with a broad set of objects belonging to a sophisticated library or framework. This typically involves initializing all of these objects, tracking dependencies, executing methods in the correct order, and so on.
As a result, the business logic of the class is tightly coupled with the implementation details of the third-party class, making it difficult to understand and maintain.
Facade is a class that provides a simple interface to a complex subsystem containing many moving parts. Facades can provide limited functionality compared to working directly with subsystems. However, this only includes features that the client really cares about.
Lets look at the examples as below.
PHP Sample Code
<?php
// 3rd party module classes
class Kitchen
{
public function cook()
{
echo "cooking order";
}
}
class Cashier
{
public function bill()
{
echo "printing bill";
}
}
//Waiter facade hiding the complexity of other 3rd party classes.
//wrapper class of our all third party packages.
class Waiter
{
public $kitchen;
public $cashier;
public function __construct()
{
$this->kitchen = new Kitchen();
$this->cashier = new Cashier();
}
//providing a simple method called order for client
public function order()
{
$this->kitchen->cook();
$this->cashier->bill();
}
}
$waiter = new Waiter();
$waiter->order();
C# Sample Code
using System;
namespace HelloWorld
{
//Waiter facade hiding the complexity of other 3rd party classes.
//wrapper class of our all third party packages.
public class Facade
{
protected Kitchen _kitchen;
protected Cashier _cashier;
public Facade()
{
this._kitchen = new Kitchen();
this._cashier = new Cashier();
}
//providing a simple method called order for client
public string order()
{
string result = "";
result += this._kitchen.cook();
result += this._cashier.bill();
return result;
}
}
// 3rd party module classes
public class Kitchen
{
public string cook()
{
return "cook order";
}
}
public class Cashier
{
public string bill()
{
return "print bill";
}
}
class Program
{
static void Main(string[] args)
{
Facade facade = new Facade();
Console.Write(facade.order());
}
}
}
By Yuuma.
yuuma at 2020年11月16日 05:40:43