PHPの実行

Jun 23, 2016 pm 02:37 PM

記事を修正してください。以前に問題を説明していませんでした。

PHP の強力な機能の 1 つである関数実行プロセスを中心に、クラスについては説明しません。 PHP のスコープ制御は関数とクラスの 2 つだけですが、実際にはスコープを制御する関数の概念が多いように感じられます。

関数はユーザー定義関数と内部関数に分かれています。内部関数は C または C++ の PHP で記述されており、ここで解析されると、モジュールの初期化時にスコープの切り替えは行われません。

内部関数、ユーザー定義関数、および op_array のデータ構造は次のとおりです:

struct _zend_op_array {	/* Common elements */	zend_uchar type;	char *function_name;			zend_class_entry *scope;	zend_uint fn_flags;	union _zend_function *prototype;	zend_uint num_args;	zend_uint required_num_args;	zend_arg_info *arg_info;	zend_bool pass_rest_by_reference;	unsigned char return_reference;	/* END of common elements */	zend_bool done_pass_two;	zend_uint *refcount;	zend_op *opcodes;	zend_uint last, size;	zend_compiled_variable *vars;	int last_var, size_var;	zend_uint T;	zend_brk_cont_element *brk_cont_array;	int last_brk_cont;	int current_brk_cont;	zend_try_catch_element *try_catch_array;	int last_try_catch;	/* static variables support */	HashTable *static_variables;	zend_op *start_op;	int backpatch_count;	zend_uint this_var;	char *filename;	zend_uint line_start;	zend_uint line_end;	char *doc_comment;	zend_uint doc_comment_len;	zend_uint early_binding; /* the linked list of delayed declarations */	void *reserved[ZEND_MAX_RESERVED_RESOURCES];};typedef struct _zend_internal_function {	/* Common elements */	zend_uchar type;	char * function_name;	zend_class_entry *scope;	zend_uint fn_flags;	union _zend_function *prototype;	zend_uint num_args;	zend_uint required_num_args;	zend_arg_info *arg_info;	zend_bool pass_rest_by_reference;	unsigned char return_reference;	/* END of common elements */	void (*handler)(INTERNAL_FUNCTION_PARAMETERS);	struct _zend_module_entry *module;} zend_internal_function;typedef union _zend_function {	zend_uchar type;	/* MUST be the first element of this struct! */	struct {		zend_uchar type;  /* never used */		char *function_name;		zend_class_entry *scope;		zend_uint fn_flags;		union _zend_function *prototype;		zend_uint num_args;		zend_uint required_num_args;		zend_arg_info *arg_info;		zend_bool pass_rest_by_reference;		unsigned char return_reference;	} common;	zend_op_array op_array;	zend_internal_function internal_function;} zend_function;typedef struct _zend_function_state {	zend_function *function;	void **arguments;} zend_function_state
ログイン後にコピー

これら 3 つのデータ構造は、上に _zend_function_state データもリストしました。 op_array の関数を実行データ _zend_execute_data の function_state フィールドの関数に割り当て、通常のコードを関数に分割する方法について説明します。スコープの切り替えについては後で説明します。

excute の実行中に、すべてを説明できる EX(function_state).function = (zend_function *) op_array; があります。

重要なデータ構造:

struct _zend_execute_data {	struct _zend_op *opline;	zend_function_state function_state;	zend_function *fbc; /* Function Being Called */	zend_class_entry *called_scope;	zend_op_array *op_array;	zval *object;	union _temp_variable *Ts;	zval ***CVs;	HashTable *symbol_table;	struct _zend_execute_data *prev_execute_data;	zval *old_error_reporting;	zend_bool nested;	zval **original_return_value;	zend_class_entry *current_scope;	zend_class_entry *current_called_scope;	zval *current_this;	zval *current_object;	struct _zend_op *call_opline;}
ログイン後にコピー

は、実行中にデータを保存するために使用され、スコープを切り替えるときに重要な役割を果たします。

ZEND_API void execute(zend_op_array *op_array TSRMLS_DC){	zend_execute_data *execute_data;	zend_bool nested = 0;	zend_bool original_in_execution = EG(in_execution);	if (EG(exception)) {		return;	}	EG(in_execution) = 1;zend_vm_enter:	/* Initialize execute_data */	execute_data = (zend_execute_data *)zend_vm_stack_alloc(		ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) +		ZEND_MM_ALIGNED_SIZE(sizeof(zval**) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)) +		ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T TSRMLS_CC);	EX(CVs) = (zval***)((char*)execute_data + ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)));	memset(EX(CVs), 0, sizeof(zval**) * op_array->last_var);	EX(Ts) = (temp_variable *)(((char*)EX(CVs)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval**) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)));	EX(fbc) = NULL;	EX(called_scope) = NULL;	EX(object) = NULL;	EX(old_error_reporting) = NULL;	EX(op_array) = op_array;	EX(symbol_table) = EG(active_symbol_table);	EX(prev_execute_data) = EG(current_execute_data);	EG(current_execute_data) = execute_data;	EX(nested) = nested;	nested = 1;	if (op_array->start_op) {		ZEND_VM_SET_OPCODE(op_array->start_op);	} else {		ZEND_VM_SET_OPCODE(op_array->opcodes);	}	if (op_array->this_var != -1 && EG(This)) { 		Z_ADDREF_P(EG(This)); /* For $this pointer */		if (!EG(active_symbol_table)) {			EX(CVs)[op_array->this_var] = (zval**)EX(CVs) + (op_array->last_var + op_array->this_var);			*EX(CVs)[op_array->this_var] = EG(This);		} else {			if (zend_hash_add(EG(active_symbol_table), "this", sizeof("this"), &EG(This), sizeof(zval *), (void**)&EX(CVs)[op_array->this_var])==FAILURE) {				Z_DELREF_P(EG(This));			}		}	}	EG(opline_ptr) = &EX(opline);	EX(function_state).function = (zend_function *) op_array;	EX(function_state).arguments = NULL;		while (1) {    	int ret;#ifdef ZEND_WIN32		if (EG(timed_out)) {			zend_timeout(0);		}#endif		if ((ret = EX(opline)->handler(execute_data TSRMLS_CC)) > 0) {			switch (ret) {				case 1:					EG(in_execution) = original_in_execution;					return;				case 2:					op_array = EG(active_op_array);					goto zend_vm_enter;				case 3:					execute_data = EG(current_execute_data);				default:					break;			}		}	}	zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");}
ログイン後にコピー

実行中、 EX(prev_execute_data) = EG(current_execute_data); でシーンが保存されます。

その後、 EG(current_execute_data) =execute_data;

関数の op_array が実行されると、 EG(active_op_array) = &EX ( function_state).function->op_array;

まで実行されます

case 2:

op_array = EG(active_op_array);
goto zend_vm_enter;

関数が実行を完了するか戻る直前に、アクティブに呼び出すことができますreturn または PHP は自動的に NULL を戻し、zend_do_return は ZEND_RETURN のオペコードを生成します。タイプに応じていくつかの異なる関数が呼び出されますが、要するに、zend_leave_helper_SPEC という名前の関数が呼び出されます。ここで、

EG(current_execute_data) = EX(prev_execute_data); 前のシーンに戻り、関数を実行する前にスコープに戻ります。

個人的には、鍵となるのは上記のデータ構造の一部とそれらの間の接続だと思います。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Laravelでフラッシュセッションデータを使用します Laravelでフラッシュセッションデータを使用します Mar 12, 2025 pm 05:08 PM

Laravelは、直感的なフラッシュメソッドを使用して、一時的なセッションデータの処理を簡素化します。これは、アプリケーション内に簡単なメッセージ、アラート、または通知を表示するのに最適です。 データは、デフォルトで次の要求のためにのみ持続します。 $リクエスト -

PHPのカール:REST APIでPHPカール拡張機能を使用する方法 PHPのカール:REST APIでPHPカール拡張機能を使用する方法 Mar 14, 2025 am 11:42 AM

PHPクライアントURL(CURL)拡張機能は、開発者にとって強力なツールであり、リモートサーバーやREST APIとのシームレスな対話を可能にします。尊敬されるマルチプロトコルファイル転送ライブラリであるLibcurlを活用することにより、PHP Curlは効率的なexecuを促進します

PHPロギング:PHPログ分析のベストプラクティス PHPロギング:PHPログ分析のベストプラクティス Mar 10, 2025 pm 02:32 PM

PHPロギングは、Webアプリケーションの監視とデバッグ、および重要なイベント、エラー、ランタイムの動作をキャプチャするために不可欠です。システムのパフォーマンスに関する貴重な洞察を提供し、問題の特定に役立ち、より速いトラブルシューティングをサポートします

Laravelテストでの簡略化されたHTTP応答のモッキング Laravelテストでの簡略化されたHTTP応答のモッキング Mar 12, 2025 pm 05:09 PM

Laravelは簡潔なHTTP応答シミュレーション構文を提供し、HTTP相互作用テストを簡素化します。このアプローチは、テストシミュレーションをより直感的にしながら、コード冗長性を大幅に削減します。 基本的な実装は、さまざまな応答タイプのショートカットを提供します。 Illuminate \ support \ facades \ httpを使用します。 http :: fake([[ 'google.com' => 'hello world'、 'github.com' => ['foo' => 'bar']、 'forge.laravel.com' =>

Codecanyonで12の最高のPHPチャットスクリプト Codecanyonで12の最高のPHPチャットスクリプト Mar 13, 2025 pm 12:08 PM

顧客の最も差し迫った問題にリアルタイムでインスタントソリューションを提供したいですか? ライブチャットを使用すると、顧客とのリアルタイムな会話を行い、すぐに問題を解決できます。それはあなたがあなたのカスタムにより速いサービスを提供することを可能にします

PHPにおける後期静的結合の概念を説明します。 PHPにおける後期静的結合の概念を説明します。 Mar 21, 2025 pm 01:33 PM

記事では、PHP 5.3で導入されたPHPの後期静的結合(LSB)について説明し、より柔軟な継承を求める静的メソッドコールのランタイム解像度を可能にします。 LSBの実用的なアプリケーションと潜在的なパフォーマ

フレームワークのカスタマイズ/拡張:カスタム機能を追加する方法。 フレームワークのカスタマイズ/拡張:カスタム機能を追加する方法。 Mar 28, 2025 pm 05:12 PM

この記事では、フレームワークにカスタム機能を追加し、アーキテクチャの理解、拡張ポイントの識別、統合とデバッグのベストプラクティスに焦点を当てています。

See all articles