技術情報

[Laravel] データベースマイグレーション シーダー編

今回はLaravelの「シーダー」(Seeder)機能を使用して初期データの投入をおこなう方法を紹介します。

続きを読む

File Operations in PHP

I will talk about file operations in PHP( handling , read, write etc.). Lets take a look at reading file first.

As an example we have a file called test.txt and if we want to read the contents within the file, can simply do like this.

<?php
echo readfile("test.txt");
//this will out put the contents within the file
?>

But what if we want to handle more than reading a file, we can use fopen

<?php

//we have to open the file first along with the operation mode.
//(r,w,a,x) etc. you can see more detail as below. 
// https://www.php.net/manual/en/function.fopen.php
$file = fopen("test.txt", "r") or die("Unable to open file!");
//then we read the file by specifying maximum number of bytes to read
echo fread($file,filesize("test.txt"));
// then simply close the file.
fclose($file);
?>

We can also get the first line in file using fgets

<?php
$file = fopen("test.txt", "r") or die("Unable to open file!");
echo fgets($file);
fclose($file);
?>

We can also use feof to check whether the file reaches to the end.

<?php
$file = fopen("test.txt", "r") or die("Unable to open file!");
// Output until end-of-file
while(!feof($file)) {
  echo fgets($file) . "<br>";
}
fclose($file);
?>

If we want to check whether file exists or not , we can do like this

<?php
echo file_exists("test.txt");
//if exists, it will return 1
?>

By Yuuma.



PHP Static methods

I will talk about static methods usages in php with some code samples. Lets get started.

Here is a sample, calling static function outside of the class.

<?php
class welcome {
  public static function sayHi() {
    echo "Welcome!";
  }
}

// we can like this -> className::methodName()
welcome::sayHi();
?>

We can also call within the class like this using self::

<?php
class welcome {
	public static function sayHi() {
		echo "Welcome!";
	}
	public function msg() {
		self::sayHi();
	}
}

$hi = new welcome();
$hi->msg();
?>

You can also call static methods from another class like this

<?php
class welcome {
  public static function sayHi() {
    echo "Welcome!";
  }
}

class anotherClass {
  public function msg() {
    welcome::sayHi();
  }
}

$var = new anotherClass();
$var->msg();
?>

You can choose any ways depending upon the situations, the result will still be the same.

By Yuuma



[Laravel] 設定ファイル「.env」の切り替えについて

単一のLaravelプロジェクトにおいてURLのパス指定により、データベースの接続先やsessionを切り分けたい場合を考えてみます。

続きを読む

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.

  1. – It is declared inside a class with the const keyword.
  2. – They are case-sensitive. It’s better to name them all upper case letter for best practice.
  3. – 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




アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム