初始化一個類別非常容易,例如下面的程式碼
MYCLASS_PROTERTY_* 這個相關是 define 的巨集字串
<code>zend_class_entry *myclass_ce; zend_function_entry myclass_methods[] = { PHP_FE_END }; PHP_MINIT_FUNCTION(myext) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, <span>"MyClass"</span>, myclass_methods); myclass_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_<span>declare</span>_class_constant_string(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_VERSION), PHP_SLIM_VERSION); zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_CONTAINER), ZEND_ACC_PUBLIC TSRMLS_CC); zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_APPS), ZEND_ACC_STATIC|ZEND_ACC_PROTECTED TSRMLS_CC); zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_NAME), ZEND_ACC_PROTECTED TSRMLS_CC); zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_ERROR), ZEND_ACC_PROTECTED TSRMLS_CC); zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_NOTFOUND), ZEND_ACC_PROTECTED TSRMLS_CC); zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_MIDDLEWARE), ZEND_ACC_PROTECTED TSRMLS_CC); <span>return</span> SUCCESS; }</code>
以上的程式碼都是一些簡單的屬性。
當想要為myclass這個類別初始化一個陣列的屬性的時候,失敗了,相對於php的程式碼如下
<code><span><span>class</span><span>MyClass</span> { public $myArray = array<span>()</span>; } /* 对应的<span>C</span>代码 */ zval *myArray; <span>MAKE_STD_ZVAL</span><span>(<span>myArray</span>)</span>; array_init<span>(<span>myArray</span>)</span>; zend_declare_property<span>(<span>myclass_ce</span>, <span>ZEND_STRL(MYCLASS_PROTERTY_NAME_MYCLASS)</span>, <span>myArray</span>, <span>ZEND_ACC_PUBLIC</span><span>TSRMLS_CC</span>)</span>;</span></code>
上面這段C程式碼變異的時候並沒有發現什麼問題,在執行new MyClass() 的時候就出現問題了,報錯如下:
<code>Internal zval<span>'s</span> can<span>'t</span> be arrays, objects <span>or</span> resources</code>
翻看zend的源代碼如下:
<code><span>if</span> (ce-><span><span>type</span> & <span>ZEND_INTERNAL_CLASS</span>) <span>{ <span>switch</span>(<span>Z_TYPE_P(property)</span>) { <span>case</span><span>IS_ARRAY</span>: <span>case</span><span>IS_CONSTANT_ARRAY</span>: <span>case</span><span>IS_OBJECT</span>: <span>case</span><span>IS_RESOURCE</span>: <span>zend_error</span>(<span>E_CORE_ERROR</span>, "<span>Internal</span><span>zval's</span><span>can't</span><span>be</span><span>arrays</span>, <span>objects</span><span>or</span><span>resources</span>"); <span>break</span>; <span>default</span>: <span>break</span>; }</span></span> }</code>
當我們在調用zend_register_internal_class的時候,myclass_ce 已經初始化為ZEND_INTERNAL_CLASS了,而此時的zend_declare_property所以產生了這樣的錯誤。
為什麼會產生這樣的錯誤呢?
我搜尋下得到的結果是:http://grokbase.com/t/php/php-internals/07a4b14xvb/php-dev-how-declare-protected-array-property-at-internal-class-properly 這個是2007年的結果,我這個是採用的php5.4版本的,暫時還是有這個問題的,文中也給了變相實現數組屬性的方法,透過在構造函數裡面實現。
<code>PHP_METHOD(myclass, __construct) { zval <span>*apps</span>, <span>*pThis</span>; pThis = getThis(); MAKE_STD_ZVAL(apps); array_init(apps); add_property_zval_ex(pThis, ZEND_STRL(SLIM_SLIM_PROTERTY_NAME_APPS), apps); }</code>
這種實作的方式對應的php程式碼
<code><span><span>class</span><span>MyClass</span> {</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>$this</span>->app = <span>array</span>(); } }</code>
以上就介紹了php擴充開發筆記(1)創建類別的陣列屬性,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。