技術情報
- 2020年05月18日
- 技術情報
Constants In PHP Object Oriented Programming
Today I will talk about constants which are very useful in development. Before we dive into code samples, there are a few things you have to remember.
- – It is declared inside a class with the const keyword.
- – They are case-sensitive. It’s better to name them all upper case letter for best practice.
- – We can access them back outside of the class with this operator (::)
Let’s see a simple constant and it has been called outside of the class.
<?php
class Welcome {
const HELLO_MSG = "Hello From Gigas!";
}
echo Welcome::HELLO_MSG;
// this will output "Hello From Gigas!"
?>
And also we can access the constant within the class like this using self keyword.
<?php
class Welcome {
const HELLO_MSG = "Hello From Gigas!";
public function sayHello() {
echo self::HELLO_MSG;
}
}
$hello = new Welcome();
$goodbye->sayHello();
// this too will output "Hello From Gigas!"
?>
By Yuuma
yuuma at 2020年05月18日 10:30:52
- 2020年05月15日
- 技術情報
[Laravel] データベースマイグレーション
Laravelのマイグレーション機能を使用することで、データベースのテーブル構造の反映をスクリプトとしてコマンドラインで実行することが可能になり、保守や変更作業などが簡単にできるようになります。
続きを読むnishida at 2020年05月15日 10:00:02
- 2020年05月11日
- 技術情報
Trait in PHP Object Oriented Programming
Today I will talk about trait and its sample usages.In the Object Oriented Inheritance, a child class can only extend only one parent class.So what if the child class has to extend more than one parent classes? OOP trait is the answer for this problem as child classes can extend many traits as they want.Check out the samples below.
Here is the sample usage of a trait
<?php
trait Trait1 {
public function print() {
echo "lets extend more than one trait!";
}
}
class Home {
use Trait1;
}
$obj = new Home();
$obj->print();
?>
Now, Lets extend more than one trait.
<?php
trait Trait1 {
public function print1() {
echo "I am the trait1";
}
}
trait Trait2 {
public function print2() {
echo "I am the trait2";
}
}
class Home {
use Trait1,Trait2;
}
$obj = new Home();
$obj->print1();
$obj->print2();
?>
By Yuuma
yuuma at 2020年05月11日 11:27:03
- 2020年05月08日
- 技術情報
[Laravel] バリデーションエラーメッセージの日本語化
nishida at 2020年05月08日 10:00:08
- 2020年05月01日
- 技術情報
[Laravel]バリデーション機能の使用
nishida at 2020年05月01日 10:00:30