{"id":88,"date":"2023-06-05T12:48:07","date_gmt":"2023-06-05T04:48:07","guid":{"rendered":"http:\/\/pro369.com\/php\/?p=88"},"modified":"2019-04-16T16:24:44","modified_gmt":"2019-04-16T16:24:44","slug":"%e8%87%aa%e5%b7%b1%e5%af%abphp%e5%bb%b6%e4%bc%b8%e4%b9%8b%e6%93%8d%e4%bd%9c%e9%a1%9e%e7%9a%84%e5%b1%ac%e6%80%a7%e5%92%8c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/por.tw\/php\/%e8%87%aa%e5%b7%b1%e5%af%abphp%e5%bb%b6%e4%bc%b8%e4%b9%8b%e6%93%8d%e4%bd%9c%e9%a1%9e%e7%9a%84%e5%b1%ac%e6%80%a7%e5%92%8c%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u81ea\u5df1\u5bebPHP\u5ef6\u4f38\u4e4b\u64cd\u4f5c\u985e\u7684\u5c6c\u6027\u548c\u65b9\u6cd5"},"content":{"rendered":"<p><font color=\"#0000FF\"><strong>\u985e\u5efa\u7acb\u597d\u4e86..<br \/> \u90a3\u9ebc\u985e\u80af\u5b9a\u4e0d\u6b62\u9019\u4e9b\u6771\u897f,\u5b83\u7531\u7e7c\u627f,\u5c6c\u6027,\u8fd4\u56de\u503c\u7b49.<\/strong><\/font><\/p>\n<p> <font color=\"#008000\"><strong>1.\u65b9\u6cd5\u7684\u53c3\u6578.<\/strong><\/font><br \/> \u6709\u65b9\u6cd5,\u8a72\u65b9\u6cd5\u5c31\u53ef\u80fd\u8981\u6709\u53c3\u6578.\u53c3\u6578\u662f\u5982\u4f55\u50b3\u905e\u904e\u4f86\u7684\u5462?<br \/> \u5982\u679c\u770b\u904e\u4e4b\u524d\u7684\u6587\u7ae0.\u90a3\u9ebc\u4f60\u80af\u5b9a\u5c31\u77e5\u9053\u4e86..\u662f\u7684 \u8207\u666e\u901a\u51fd\u6578\u7684\u53c3\u6578\u76f8\u540c.<br \/> \u8072\u660echar\u985e\u578b\u7684\u6307\u6a19\u7528\u4f86\u5132\u5b58\u53c3\u6578\u7684\u503c.<br \/> \u8072\u660eint\u985e\u578b\u7684\u8b8a\u91cf\u4f86\u5132\u5b58\u7684\u53c3\u6578\u9577\u5ea6<br \/> \u7136\u5f8c\u7528\u5230 zend_parse_parameters\u51fd\u6578<br \/> zend_parse_parameters(&#8220;,`&lt; char *type_spec&gt;`,`&lt; &#8230;&gt;`)<br \/> \u8a72\u53c3\u6578\u6709\u5e7e\u500b\u91cd\u8981\u7684\u53c3\u6578<br \/> \u7b2c\u4e00\u500b\u662f\u53c3\u6578\u7684\u500b\u6578<br \/> \u7b2c\u4e8c\u500b\u6bd4\u8f03\u91cd\u8981,\u5b83\u6307\u5b9a\u63a5\u6536\u53c3\u6578\u7684\u985e\u578b<br \/> \u4e0b\u9762\u9019\u4efd\u6e05\u55ae\u5b8c\u6574\u5730\u5217\u8209\u51fa\u4e86\u6211\u5011\u53ef\u4ee5\u6307\u5b9a\u63a5\u6536\u7684\u53c3\u6578\u985e\u578b\uff1a<\/p>\n<p> l &ndash; \u9577\u6574\u6578<\/p>\n<p> d &ndash; \u96d9\u7cbe\u5ea6\u6d6e\u9ede\u6578<\/p>\n<p> s &ndash; \u5b57\u4e32 (\u4e5f\u53ef\u80fd\u662f\u7a7a\u4f4d\u5143\u7d44)\u548c\u5176\u9577\u5ea6<\/p>\n<p> b &ndash; \u5e03\u6797\u503c<\/p>\n<p> r &ndash; \u8cc7\u6e90, \u5132\u5b58\u5728 zval*<\/p>\n<p> a &ndash; \u9663\u5217, \u5132\u5b58\u5728 zval*<\/p>\n<p> o &ndash; \uff08\u4efb\u4f55\u985e\u7684\uff09\u5c0d\u50cf, \u5132\u5b58\u5728 zval*<\/p>\n<p> O &ndash; \uff08\u7531class entry \u7279\u6b8a\u7684\u985e\u7684\uff09\u5c0d\u50cf, \u5132\u5b58\u5728 zval*<\/p>\n<p> z &ndash; \u5be6\u969b\u7684 zval*<\/p>\n<p> \u4e0b\u9762\u7684\u4e00\u4e9b\u5b57\u5143\u5728\u985e\u578b\u8aaa\u660e\u5b57\u4e32\uff08\u5c31\u662f\u90a3\u500b char *type_spec\uff09\u4e2d\u5177\u6709\u7279\u5225\u7684\u542b\u7fa9\uff1a<\/p>\n<p> | &ndash; \u986f\u793a\u5269\u4e0b\u7684\u53c3\u6578\u90fd\u662f\u53ef\u9078\u53c3\u6578\u3002\u5982\u679c\u4f7f\u7528\u8005\u6c92\u6709\u50b3\u9032\u4f86\u9019\u4e9b\u53c3\u6578\u503c\uff0c\u90a3\u9ebc\u9019\u4e9b\u503c\u5c31\u6703\u88ab\u8d77\u59cb\u5316\u6210\u9810\u8a2d\u503c\u3002<\/p>\n<p> \/ &ndash; \u986f\u793a\u53c3\u6578\u89e3\u6790\u51fd\u6578\u5c07\u6703\u5c0d\u5269\u4e0b\u7684\u53c3\u6578\u4ee5 SEPARATE_ZVAL_IF_NOT_REF() \u7684\u6a21\u5f0f\u4f86\u63d0\u4f9b\u9019\u500b\u53c3\u6578\u7684\u4e00\u4efd\u8986\u5236\uff0c\u9664\u975e\u9019\u4e9b\u53c3\u6578\u662f\u4e00\u500b\u5f15\u7528\u3002<\/p>\n<p> ! &ndash; \u986f\u793a\u5269\u4e0b\u7684\u53c3\u6578\u5bb9\u8a31\u88ab\u8a2d\u5b9a\u70ba NULL\uff08\u50c5\u7528\u5728 a\u3001o\u3001O\u3001r\u548cz\u8eab\u4e0a\uff09\u3002\u5982\u679c\u4f7f\u7528\u8005\u50b3\u9032\u4f86\u4e86\u4e00\u500b NULL \u503c\uff0c\u5247\u5b58\u5132\u8a72\u53c3\u6578\u7684\u8b8a\u91cf\u5c07\u6703\u8a2d\u5b9a\u70ba NULL\u3002<br \/> \u51fd\u6578 get\u63a5\u6536\u5169\u500b\u53c3\u6578 ,\u7b2c\u4e00\u500b\u53c3\u6578\u662f\u5fc5\u586b\u5b57\u4e32\u985e\u578b,\u7b2c\u4e8c\u500b\u53c3\u6578\u662f\u53ef\u9078\u70ba\u6574\u578b.<br \/> PHP\u4ee3\u78bc\u5982\u4e0b<br \/> 1<br \/> 2<br \/> 3<br \/> &nbsp;&nbsp; &nbsp;<br \/> function get($name,$age=0){<br \/> &nbsp;<br \/> }<\/p>\n<p> \u5982\u679c\u8981\u5728\u5ef6\u4f38\u88e1\u5be6\u73fe\u9019\u6a23\u7684\u529f\u80fd,zend_parse_parameters\u7684\u4f7f\u7528\u5982\u4e0b<br \/> char* name;<br \/> int name_length;<br \/> int age;<br \/> int age_length;<br \/> zend_parse_parameters(ZEND_NUM_ARGS ,\u300ds|l\u300d,&amp;name,&amp;name_length,&amp;age,&amp;age_len)<br \/> zend_parse_parameters \u57f7\u884c\u5931\u6557\u7684\u8a71\u6703\u8fd4\u56deFAILURE.<br \/> \u57f7\u884c\u5b8c\u6210\u5f8c,name\u5c31\u662f\u7b2c\u4e00\u500b\u53c3\u6578\u7684\u503c,age\u5c31\u662f\u7b2c\u4e8c\u500b\u53c3\u6578\u7684\u503c<br \/> \u6211\u5011\u751a\u81f3\u53ef\u4ee5\u547c\u53ebphp_printf\u51fd\u6578\u4f86\u8f38\u51fa.<\/p>\n<p> 2.\u985e\u7684\u5c6c\u6027<br \/> \u9019\u985e\u7684\u5167\u5bb9\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\u5176\u5be6\u5df2\u7d93\u8b1b\u6e05\u695a\u4e86.<br \/> \u7528zend_declare_property_*\u4e00\u7cfb\u5217\u51fd\u6578\u4f86\u5efa\u7acb\u6211\u5011\u7684\u5c6c\u6027.\u7576\u7136\u662f\u5728PHP_MINIT_FUNCTION\u51fd\u6578\u4e2d\u5efa\u7acb<br \/> \u4ee5 zend_declare_property_null\u70ba\u4f8b:<br \/> zend_declare_property_null(&#8220;,`&lt; char *name&gt;`,`&lt; int name_length&gt;`,`&lt; int access_type TSRMLS_DC&gt;`)<br \/> ce :\u662f\u4e00\u500bzend_class_entry\u7684\u6307\u6a19.<br \/> name :\u662f\u5c6c\u6027\u7684\u540d\u7a31<br \/> length :\u5c6c\u6027\u540d\u7a31\u7684\u9577\u5ea6<br \/> access_type:\u5c6c\u6027\u7684\u8a2a\u554f\u5c64\u7d1a.<br \/> \u9084\u6709\u5176\u4ed6\u5e7e\u500b\u51fd\u6578 \u7528\u6cd5\u4e00\u6a23.<br \/> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> &nbsp;&nbsp; &nbsp;<br \/> zend_declare_property_bool<br \/> zend_declare_property_double<br \/> zend_declare_property_ex<br \/> zend_declare_property_long<br \/> zend_declare_property_null<br \/> zend_declare_property_string<br \/> zend_declare_property_stringl<\/p>\n<p> 3.\u8b80\u53d6\u5c6c\u6027<br \/> \u5efa\u7acb\u4e86\u5c6c\u6027,\u90a3\u9ebc\u8a72\u5982\u4f55\u8b80\u53d6\u6b64\u5c6c\u6027\u5462?<br \/> zend_read_property(&#8220;,`&lt; zval *object&gt;`,`&lt; char *name&gt;`,`&lt; int name_length&gt;`,`&lt; zend_bool silent TSRMLS_DC&gt;`);<br \/> \u5c31\u8981\u4f7f\u7528\u9019\u500b\u51fd\u6578\u4e86,\u5b83\u5c07\u53d6\u5f97\u5230\u7684\u5c6c\u6027\u8a0a\u606f\u8fd4\u56de\u5230\u4e00\u500bzval\u7d50\u69cb\u9ad4\u4e2d<br \/> \u9019\u6642\u5019\u8981\u7528\u5230\u51fd\u6578getThis();<br \/> \u9019\u500b\u51fd\u6578\u6703\u8fd4\u56de\u4e00\u500bzval\u7684\u6307\u6a19.<br \/> \u56e0\u70ba\u662f\u4e00\u500b\u5c0d\u50cf \u6240\u4ee5\u5132\u5b58\u5728\u7d50\u69cb\u9ad4_zend_object_value \u4e2d<br \/> typedef struct _zend_object_value {<br \/> zend_object_handle handle;<br \/> zend_object_handlers *handlers;<br \/> } zend_object_value;<br \/> \u7b2c\u4e00\u500b\u53c3\u6578\u6703\u547c\u53eb zend_object_handler\u6240\u6307\u5411\u7684\u51fd\u6578\u6307\u6a19get_class_entry\u4f86\u53d6\u5f97\u73fe\u7528\u7684\u57f7\u884c\u7684\u985e\u7684\u8a0a\u606f.<br \/> \u7b2c\u4e00\u500b\u53c3\u6578\u61c9\u8a72\u662f Z_OBJEC_P(getThis());<br \/> \u7b2c\u4e8c\u500b\u53c3\u6578\u662f getThis();<br \/> \u5b83\u5b9a\u7fa9\u70ba #define getThis() (this_ptr);<br \/> this_ptr\u662f\u4ec0\u9ebc\u6771\u897f\u6211\u9084\u4e0d\u78ba\u5b9a,\u6211\u60f3\u61c9\u8a72\u662fPHP\u5167\u6838\u7dad\u8b77\u7684\u4e00\u500b\u5132\u5b58\u985e\u8a0a\u606f\u7684zval\u5c0d\u50cf.\u56de\u982d\u518d\u597d\u597d\u7814\u7a76\u7814\u7a76.<br \/> \u7b2c\u4e09\u500b\u53c3\u6578\u662f\u8981\u53d6\u5f97\u7684\u5c6c\u6027<br \/> \u7b2c\u56db\u500b\u53c3\u6578\u662f\u5c6c\u6027\u7684\u9577\u5ea6.<br \/> \u4fee\u6539\u4e4b\u5f8c\u7684getproperty\u65b9\u6cd5<br \/> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> &nbsp;&nbsp; &nbsp;<br \/> PHP_METHOD(Person,getproperty){<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; zval* self=getThis();<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; zval* name;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name=zend_read_property(Z_OBJCE_P(self),self,ZEND_STRL(&quot;name&quot;),0 TSRMLS_CC);<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; php_printf(&quot;%s&quot;,name-&gt;value.str.val);<br \/> }<\/p>\n<p> 4.\u8a2d\u5b9a\u5c6c\u6027<br \/> \u8a72\u5982\u4f55\u66f4\u65b0\u5c6c\u6027\u7684\u503c\u5462?<br \/> \u540c\u6a23\u7d66\u6211\u5011\u63d0\u4f9b\u4e86\u4e00\u500b\u51fd\u6578<br \/> zend_update_property(&#8220;,`&lt; zval *object&gt;`,`&lt; char *name&gt;`,`&lt; int name_length&gt;`,`&lt; zval *value TSRMLS_DC&gt;`)<br \/> \u524d\u56db\u500b\u53c3\u6578\u8207zend_read_property\u76f8\u540c.<br \/> \u4e0d\u540c\u7684\u662f\u6700\u5f8c\u4e00\u500bzval*value.\u5b83\u662f\u6211\u5011\u5c6c\u6027\u7684\u503c..\u56e0\u70ba\u4ed6\u662f\u4e00\u500bzval\u7684\u6307\u6a19,\u6240\u4ee5\u6211\u5011\u9700\u8981\u5c07\u901a\u904ezend_parse_parameters\u53d6\u5f97\u5230\u7684\u53c3\u6578\u8f49\u70ba\u4e00\u500bzval\u7684\u6307\u6a19.<br \/> \u4fee\u6539\u5f8c\u7684setproperty\u65b9\u6cd5.<br \/> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/> 10<br \/> 11<br \/> 12<br \/> 13<br \/> &nbsp;&nbsp; &nbsp;<br \/> PHP_METHOD(Person,setproperty){<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *key=NULL;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int key_len;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *val=NULL;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int val_len;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; zval* self=getThis();<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; zval* value;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,&quot;ss&quot;,&amp;key,&amp;key_len,&amp;val,&amp;val_len)==FAILURE){<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MAKE_STD_ZVAL(value);<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ZVAL_STRINGL(value,val,val_len,1);<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; zend_update_property(Z_OBJCE_P(self),self,key,key_len,value TSRMLS_CC);<\/p>\n<p> \u6700\u5f8cmake &amp;&amp; make install,\u91cd\u555fapache.<br \/> \u9019\u6a23\u6211\u5011\u5728 php\u4ee3\u78bc\u88e1.<br \/> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> &nbsp;&nbsp; &nbsp;<br \/> &lt;?php<br \/> $a=new Person();<br \/> $a-&gt;setproperty(&quot;name&quot;,&quot;this is siren&quot;);<br \/> $a-&gt;getproperty();<br \/> ?&gt;<\/p>\n<p> \u5c31\u53ef\u4ee5\u8f38\u51fa\u6211\u5011\u901a\u904esetproperty\u8a2d\u5b9a\u7684\u503c\u4e86.<br \/> <strong><br \/> \u8072\u660e\uff1a\u672c\u6587\u70ba\u65af\u4eba\u539f\u5275\uff0c\u5168\u90e8\u70ba\u4f5c\u8005\u4e00\u4e00\u5206\u6790\u5f97\u4e4b\uff0c\u6709\u4e0d\u5c0d\u7684\u5730\u65b9\u671b\u8cdc\u6559\u3002<br \/> \u6b61\u8fce\u8f49\u8f09\uff0c\u8f49\u8f09\u8acb\u8a3b\u660e\u51fa\u8655 \u3002<br \/> \u672c\u6587\u4f4d\u5740\uff1ahttp:\/\/imsiren.com\/archives\/581<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u985e\u5efa\u7acb\u597d\u4e86.. \u90a3\u9ebc\u985e\u80af\u5b9a\u4e0d\u6b62\u9019\u4e9b\u6771\u897f,\u5b83\u7531\u7e7c\u627f,\u5c6c\u6027,\u8fd4\u56de\u503c\u7b49. 1.\u65b9\u6cd5\u7684\u53c3\u6578. \u6709\u65b9\u6cd5,\u8a72\u65b9\u6cd5\u5c31\u53ef\u80fd\u8981\u6709 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":400,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[22],"class_list":["post-88","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/posts\/88"}],"collection":[{"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":0,"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/media\/400"}],"wp:attachment":[{"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/por.tw\/php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}