PHPでクラスのインスタンスを作成する際、他のクラスを親に持つクラスであった場合、メモリの使用量はどうなるのか?を見ることにする。
実行せずともなんとなく結果は予想できるけれども、PHPのVLDでfor文を見る2のようなことがあるので、一応確認しておく。
obj.php
<?php
class Animal {
function bark(){
return "ばう";
}
}
class Cat {
}
echo memory_get_usage() . "\n";
$cat = new Cat();
echo memory_get_usage() . "\n";
はじめに上記のように2つのクラスがあるけれども、親子関係がない状態でこのコードを実行してみると、
$ php /path/to/dir/obj.php 398720 398792
インスタンスのメモリの使用量が72となった。
続いて、
<?php
class Animal {
function bark(){
return "ばう";
}
}
class Cat extends Animal {
}
echo memory_get_usage() . "\n";
$cat = new Cat();
echo memory_get_usage() . "\n";
CatクラスでAnimalクラスを親として継承するという書き方に変更して実行してみると、
$ php /path/to/dir/obj.php 399040 399112
全体の使用量は変わったが、差は先程と同じ72であった。
親クラスだけのインスタンスを作成したらどうだろう?と下記のコードを作成し、実行してみたら、
<?php
class Animal {
function bark(){
return "ばう";
}
}
class Cat extends Animal {
}
echo memory_get_usage() . "\n";
$cat = new Animal();
echo memory_get_usage() . "\n";
差はAnimalを継承したCatクラスのインスタンスの作成と同じ差が72であった。
クラスからオブジェクト型のデータを作成して変数に挿入する際、継承元やクラスメソッドの情報を持たずにオブジェクトとプロパティ値のみ持つといったところだろうか。
関数やクラスメソッドを含め、オブジェクトがどのようにデータを持つか把握する必要がある。
せっかくなので、下記のコードをVLDで確認してみると、
<?php
class Animal {
function bark(){
return "ばう";
}
}
class Cat extends Animal {
}
$ php -d vld.active=1 -d.vld.execute=0 /path/to/dir/obj.php
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: /path/to/dir/obj.php
function name: (null)
number of ops: 1
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
12 0 E > > RETURN 1
branch: # 0; line: 16- 16; sop: 0; eop: 0; out0: -2
path #1: 0,
Class Animal:
Function bark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: /path/to/dir/obj.php
function name: bark
number of ops: 5
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
4 0 E > EXT_NOP
5 1 EXT_STMT
2 > RETURN '%E3%81%B0%E3%81%86'
6 3* EXT_STMT
4* > RETURN null
branch: # 0; line: 4- 6; sop: 0; eop: 4
path #1: 0,
End of function bark
End of class Animal.
Class Cat:
Function bark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: /path/to/dir/obj.php
function name: bark
number of ops: 5
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
4 0 E > EXT_NOP
5 1 EXT_STMT
2 > RETURN '%E3%81%B0%E3%81%86'
6 3* EXT_STMT
4* > RETURN null
branch: # 0; line: 4- 6; sop: 0; eop: 4
path #1: 0,
End of function bark
End of class Cat.
Catクラスがbarkメソッドを持っている事になっている。
継承は指定のクラスに任意のメソッドがなければ、親クラスを辿ってあれば返すというイメージがあったけれども、インスタンス作成時点でうまい具合に組み立てているということなのかな?
では、CatクラスでAnimalクラスのbarkメソッドを上書きするようにコードを書いてみたらどうだろう?ということで、
<?php
class Animal {
function bark(){
return "ばう";
}
}
class Cat extends Animal {
function bark(){
return "にゃ〜ん";
}
}
$ php -d vld.active=1 -d.vld.execute=1 /path/to/dir/obj.php
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: /path/to/dir/obj.php
function name: (null)
number of ops: 1
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
15 0 E > > RETURN 1
branch: # 0; line: 15- 15; sop: 0; eop: 0; out0: -2
path #1: 0,
Class Animal:
Function bark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: /path/to/dir/obj.php
function name: bark
number of ops: 5
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
4 0 E > EXT_NOP
5 1 EXT_STMT
2 > RETURN '%E3%81%B0%E3%81%86'
6 3* EXT_STMT
4* > RETURN null
branch: # 0; line: 4- 6; sop: 0; eop: 4
path #1: 0,
End of function bark
End of class Animal.
Class Cat:
Function bark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: /path/to/dir/obj.php
function name: bark
number of ops: 5
compiled vars: none
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
11 0 E > EXT_NOP
12 1 EXT_STMT
2 > RETURN '%E3%81%AB%E3%82%83%E3%80%9C%E3%82%93'
13 3* EXT_STMT
4* > RETURN null
branch: # 0; line: 11- 13; sop: 0; eop: 4
path #1: 0,
End of function bark
End of class Cat.
barkの処理がCatのbarkに書き換わっていた。





