アプリ関連ニュース
- 2021年3月24日
- 他の話題
NFT(非代替トークン)とは何か調べました
tanaka at 2021年03月24日 10:00:17
- 2021年3月22日
- 技術情報
Magic methods in PHP – Part 2
I will continue to talk about magic methods article I wrote last week. For the part 1, you can read here.
ToString
The magic __toString () method allows you to define what you would like to display when an object of the class is treated as a string. If you use echo or print on your object, and you haven’t defined the __toString () method, it will give an error.
<?php
class Student {
private $name;
public function __construct($name)
{
$this->name = $name;
$this->email = $email;
}
public function __toString()
{
return 'Name: '.$this->name;
}
}
$student = new Student('Yuuma');
echo $student;
?>
Call
If the __get () and __set () methods are called for non-existent properties, the __call () method is called when trying to invoke inaccessible methods, the methods that have not been defined in your class.
<?php
class Student {
public function __call($methodName, $arg)
{
// $methodName = getName
// $arg = array('1')
}
}
$student = new Student();
$student->getName(1);
?>
Isset
The magic __isset () method is called when you call the isset () method on inaccessible or non-existent object properties. Let’s see how it works with an example.
<?php
class Student {
private $data = array();
public function __isset($name)
{
return isset($this->data[$name]);
}
}
$student = new Student();
echo isset($student->email);
?>
Invoke
The magic __invoke () method is a special method that is called when you try to call an object as if it were a function. the $student object is treated as if it were a function, and since we have defined the __invoke () method, it will be called instead of giving you an error. The main purpose of the __invoke () method is that if you want to treat your objects as callable, you can implement this method.
<?php
class Student {
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function __invoke()
{
echo 'Invoke function';
}
}
$student = new Student('John');
$student();
?>
There are still some magic methods in PHP but let me stop here as I had written most used methods in article part 1 and part 2. If you are interest for more, you can check it out in PHP official documentation.
Yuuma
yuuma at 2021年03月22日 11:00:59
- 2021年3月19日
- 技術情報
Tips to Speed up your database queries in Laravel
Here is some tips based on my experience to speed up database queries.
Table Indexing
Indexes are important part in database. Indexes are used to quickly retrieve data without having to search every row in a database table is accessed. The users cannot see the indexes, they are just used to speed up searches/queries.
How to use? For example, you have thousands of rows of users and you want to get an email.
Select * from users where email = “me@gmail.com”
Without index, this process will be from top to bottom until it the email is found.
Using with index, in Laravel, the easy way is to create a migration file.
public function up()
{
Schema::table(‘users’, function(Blueprint $table)
{
$table->index(‘email’);
});
}
Or if you currently on fresh project just add index to column you want to
public function up()
{
Schema::create(‘users, function(Blueprint $table) {
…
$table->index(‘email’);
…
});
}
Index is not only for single column. You can use multiple column like this $table->index([‘email’, ‘ phone_no’]); Let’s us look at a query that could use two different indexes on the table based on the WHERE clause restrictions. Multi column indexes are called compound indexes.
Eloquent Eager Loading
At its core Eager Loading, is telling Eloquent that you want to grab a model with specific relationships that way the framework produces a more performant query to grab all the data you will need. By eager loading, you can take many queries down to just one or two. Wrong implementation cause N+1. Here is where common developer do which cause N+1.
For example:
Class Post extends Model
{
public function author()
{
return $this->belongsTo(Author::class);
}
}
Lets fetch author about 100 rows from the table and what happen
Mistake 1
$post = Post::all();
foreach($posts as $post)
{
$total_user = $post->author;
}
The query runs once to get all the author and for each loop it query another query for author for 100 times. 100 + 1 = N+1 problem. Imagine if you want to fetch thousand to data. If one query cost 100ms * 100 = 10,000ms it takes to complete process.
Solution
It can be solve by using with method in Eager loading. For example
$post = App\Post::with(‘author’)->get();
If you had multiple related associations, you can eager load them with an array:
$post = App\Post::with([‘author’, ‘comments’])->get();
$post =App\Post::with(‘author.profile’)->get();
With this, instead of hitting database N+1, with will single query by joining those table and transform it into collection. So you can freely access the collection object without hitting the database.
By Ami
asahi at 2021年03月19日 10:00:36
- 2021年3月18日
- Unity
[Unity] UI Canvas の使用方法(1)
nishida at 2021年03月18日 10:00:59
- 2021年3月17日
- 他の話題
全ポート10GBASE-T対応の家庭用スイッチングハブがリリースされていました
tanaka at 2021年03月17日 10:00:06