çåºå«ï¼
ä¸ä¸ªæprotectedæååéï¼å设为iï¼çç±»åBaseï¼ä»çåç±»Derivedç´æ¥ç»§æ¿èªBaseï¼åæ¶Derivedæä¸ä¸ªæåå½æ°funcï¼ä»çåæ°æ¯Baseç±»åçï¼è¿ä¸ªå½æ°å¯ä»¥è®¿é®Base对象çprotecteçæååï¼å¨C++ä¸ä¸å¯ä»¥ï¼èå¨PHPä¸å¯ä»¥ã
å
³äºéè½½ï¼
PHPä¸ä¸æ¯æC++ä¸çéè½½ï¼éæ°å®ä¹ä¸ªå½æ°ï¼ä¸ç®¡æ¯åæ°ç¸åè¿æ¯ä¸åï¼é½ä¼å¯¼è´éå¤å®ä¹é误ï¼ãPHPæèªå·±çéè½½æ¹æ³ï¼èä¸æ¯C++ä¸çæ´å¹¿æ³ï¼ä»ä¸ä»
å¯ä»¥éè½½å½æ°ï¼éè¿ä½¿ç¨__call()æ¹æ³ï¼ï¼è¿å¯ä»¥éè½½æååéï¼éè¿ä½¿ç¨__get()å__set()ï¼ãèC++ä¸åªè½å¯¹å½æ°è¿è¡éè½½ï¼èä¸éè½½çæ¹æ³åªæï¼ä¸åçåæ°ç±»åï¼ä¸åçå½æ°ä¿®é¥°ç¬¦ï¼constï¼ã
å
³äºabstractåinterface
PHPä¸æè¿ä¸¤ä¸ªä¿®é¥°ç¬¦ï¼å 为PHPä¸æ¯æ对ä¸ä¸ªæ®éç±»çå¤ç»§æ¿ï¼å æ¤interfaceç¨äºå®ç°å¤ç»§æ¿ãAbstractç±»å¨C++ä¸ä¹åå¨ï¼åªæ¯å®ç°æ¹å¼ä¸ä¸æ ·ï¼C++ä¸ä½¿ç¨pure virtualå½æ°æ¥è¡¨æè¿ä¸ªç±»æ¯æ½è±¡ç±»ï¼ä¸è½åç¬è¢«å®ä¾åãèPHPä¸ä¸ä»
å¯ä»¥ä½¿ç¨å½æ°ç修饰符abstractï¼å¦å¤classåä¹å¿
é¡»æ æabstractï¼æ¥æ æè¿ä¸ªææï¼ä¹å¯ä»¥åªä½¿ç¨abstract修饰符修饰classæ´ç´æ¥ç表æä»æ¯æ½è±¡ç±»ãå¦å¤ï¼ C++ä¸çpure virtualå½æ°ä¸å访é®ç±»åçéå¶ï¼ä¸ç®¡æ¯ä»ä¹è®¿é®ç±»åï¼é½ä¼è¢«çä½æ¯publicï¼èphpä¸çabstractå½æ°ä¸è½è¢«å£°ææprivateï¼ä»
æ¤èå·²ã
å¤æ
å 为PHPæ¯å¼±ç±»åè¯è¨ï¼æ以ä»çå¤ææ§å°å¤é½æä½ç°ï¼å¯¼è´ä»çå¤ææ§ä¸åC++ä¸é£ä¹ææ¾ãæ¯å¦ï¼å¨PHPä¸åºç±»çå½æ°å¯ä»¥çä½å
¨æ¯virtualçï¼å æ¤å®ä¸éè¦å ä»»ä½ä¿®é¥°ç¬¦ï¼åç±»ä¸ååºç±»ååçå½æ°é½ä¼è¢«å¨æè°ç¨ï¼èC++ä¸ä¸æ ·ï¼å¦æåºç±»ä¸çè¿ä¸ªå½æ°æ²¡æå virtual修饰符ï¼åç±»ä¸çé£ä¸ªååå½æ°å°±ä¸ä¼è¢«å¨æè°ç¨ï¼åªè½éæè°ç¨äºã
æä½ç¬¦éè½½
PHPä¸ä¸åå¨ï¼èC++åå¨ãéç¹å¨==æä½ç¬¦ï¼å¨PHPä¸ä»å¯ä»¥è¿ç¨å¨ä»»ä½ç±»åä¸ï¼å³ä½¿è¿ä¸ªç±»å没æï¼åC++ä¸ä¸æ ·ï¼åèªå·±ç==éè½½å½æ°ãPHPä¸å¯¹äºå¯¹è±¡çæ¯è¾ï¼==表示两个对象çå±æ§åå¼é½ä¸æ ·ï¼èä¸ç±»åä¹ä¸æ ·ï¼PHPä¹åå¨ä¸ä¸ª===æä½ç¬¦ï¼è¡¨ç¤ºä»ä»¬å¼ç¨äºåä¸ä¸ªå¯¹è±¡ï¼è¿è·javaä¸å¾åå§ã
finalå
³é®å
PHPä¸åå¨è¿ä¸ªå
³é®åï¼è¡¨ç¤ºè¿ä¸ªå½æ°ä¸è½è¢«éåï¼å¦æä»ç¨æ¥ä¿®é¥°å½æ°ï¼ï¼æè
ç±»ä¸è½è¢«ç»§æ¿ï¼å¦æç¨ç±»ä¿®é¥°classï¼ãèC++ä¸æ²¡æè¿ä¸ªå
³é®åï¼ä¹æ æ³æ¨¡æåºé£ä¸ªææã
对象èµå¼åæ·è´
C++ä¸çæ·è´æè
èµå¼å¤§è´æè¿ä¸ç§æ¹å¼ã
ä¸ç§æ¯æéçèµå¼ï¼å³p1 = &objï¼ç¸å½äºPHPåjavaä¸çèµå¼æä½ï¼
å¦ä¸ç§æ¯memberwise assignmentï¼ä¹å°±æ¯å¨obj1 = obj2æ¶åççäºæ ï¼é»è®¤æ åµä¸æ§è¡æµ 度æ·è´ï¼åPHPä¸cloneçææä¸æ ·ãä»å¯ä»¥éè¿éè½½èµå¼æ·è´æä½æ¥æ§è¡ä½ ç深度æ·è´æè å ¶ä»èªå®ä¹çæ·è´ï¼è¿å°±ç¸å½äºPHPä¸ç__clone()æåå½æ°ï¼
第ä¸ç§æ¯memberwise initializationï¼ä¹å°±æ¯å¨åæ°ä¼ éæ¶ï¼ä¼ éè¿åå¼æ¶æè å å«å¯¹è±¡ç容å¨åå§åçæ¶åèªå¨è¿è¡çï¼ä½ å¯ä»¥éè¿èªå·±å®ä¹æ·è´æé å½æ°æ¥æ§å¶å®çææã
æ·è´æé å½æ°å¨PHPä¸å ä¹ç¨ä¸å°ï¼å 为ä»çä¼ éæ¯å®å ¨æå¼ç¨ä¼ éï¼èä¸æ¯å¯¹è±¡çç´æ¥æ·è´ã