ÕýÈ·Àí½âPHPÃæÏò¶ÔÏóÖеÄthis¡¢self¡¢parent
Ò»¡¢PHPÖÐthis¡¢self¡¢parentµÄÇø±ðÖ®Ò»
ÃæÏò¶ÔÏó±à³Ì(OOP,Object Oriented Programming)ÏÖÒѾ³ÉΪ±à³ÌÈËÔ±µÄÒ»Ïî»ù±¾¼¼ÄÜ¡£ÀûÓÃOOPµÄ˼Ïë½øÐÐPHPµÄ¸ß¼¶±à³Ì£¬¶ÔÓÚÌá¸ßPHP±à³ÌÄÜÁ¦ºÍ¹æ»®web¿ª·¢¹¹¼Ü¶¼ÊǺÜÓÐÒâÒåµÄ¡£
PHP5¾¹ýÖØдºó£¬¶ÔOOPµÄÖ§³Ö¶îÓÐÁ˺ܴóµÄ·ÉÔ¾£¬³ÉΪÁ˾߱¸Á˴󲿷ÖÃæÏò¶ÔÏóÓïÑÔµÄÌØÐÔµÄÓïÑÔ£¬±ÈPHP4ÓÐÁ˺ܶàµÄÃæÏò¶ÔÏóµÄÌØÐÔ¡£ÕâÀïÎÒ Ö÷Ҫ̸µÄÊÇ this,self,parent Èý¸ö¹Ø¼ü×ÖÖ®¼äµÄÇø±ð¡£´Ó×ÖÃæÉÏÀ´Àí½â£¬·Ö±ðÊÇÖ¸ Õâ¡¢×Ô¼º¡¢¸¸Çס£Ïȳõ²½½âÊÍһϣ¬thisÊÇÖ¸Ïòµ±Ç°¶ÔÏóµÄÖ¸Õ루¿ÉÒÔ¿´³ÉCÀïÃæµÄÖ¸Õ룩£¬selfÊÇÖ¸Ïòµ±Ç°ÀàµÄÖ¸Õ룬parentÊÇÖ¸Ïò¸¸ÀàµÄÖ¸Õë¡£ ÎÒÃÇÕâÀïƵ·±Ê¹ÓÃÖ¸ÕëÀ´ÃèÊö£¬ÊÇÒòΪûÓиüºÃµÄÓïÑÔÀ´±í´ï¡£¹ØÓÚÖ¸ÕëµÄ¸ÅÄ´ó¼Ò¿ÉÒÔÈ¥²Î¿¼°Ù¿Æ¡£
ÏÂÃæÎÒÃǾ͸ù¾Ýʵ¼ÊµÄÀý×Ó½áºÏÀ´½²½²¡£
<?php class name //½¨Á¢ÁËÒ»¸öÃûΪnameµÄÀà { private $name; //¶¨ÒåÊôÐÔ£¬Ë½ÓÐ //¶¨Òå¹¹Ô캯Êý£¬ÓÃÓÚ³õʼ»¯¸³Öµ function __construct( $name ) { $this->name = $name; //ÕâÀïÒѾʹÓÃÁËthisÖ¸ÕëÓï¾ä¢Ù } //Îö¹¹º¯Êý function __destruct(){ } //´òÓ¡Óû§Ãû³ÉÔ±º¯Êý function printname() { print( $this->name ); //ÔÙ´ÎʹÓÃÁËthisÖ¸ÕëÓï¾ä¢Ú£¬Ò²¿ÉÒÔʹÓÃechoÊä³ö } } $obj1 = new name( "PBPHome" ); //ʵÀý»¯¶ÔÏó Óï¾ä¢Û //Ö´ÐдòÓ¡ $obj1->printname(); //Êä³ö: PBPHome echo "<br>"; //Êä³ö£º»Ø³µ //µÚ¶þ´ÎʵÀý»¯¶ÔÏó $obj2 = new name( "PHP" ); //Ö´ÐдòÓ¡ $obj2->printname(); //Êä³ö£ºPHP ?>˵Ã÷£ºÉÏÃæµÄÀà·Ö±ðÔÚ Óï¾ä¢ÙºÍÓï¾ä¢ÚʹÓÃÁËthisÖ¸Õ룬ÄÇôµ±Ê±thisÊÇÖ¸ÏòËÄØ£¿ÆäʵthisÊÇÔÚʵÀý»¯µÄʱºòÀ´È·¶¨Ö¸ÏòË£¬±ÈÈçµÚÒ»´ÎʵÀý»¯¶ÔÏóµÄʱºò(Óï¾ä¢Û)£¬ÄÇ Ã´µ±Ê±this¾ÍÊÇÖ¸Ïò$obj1¶ÔÏó£¬ÄÇôִÐÐÓï¾ä¢ÚµÄ´òӡʱ¾Í°Ñ print( $this-><name ) ±ä³ÉÁË print( $obj1->name )£¬ÄÇôµ±È»¾ÍÊä³öÁË¡±PBPHome¡±¡£µÚ¶þ¸öʵÀýµÄʱºò£¬print( $this->name )±ä³ÉÁËprint( $obj2->name )£¬ÓÚÊǾÍÊä³öÁË¡±PHP¡±¡£ËùÒÔ˵£¬this¾ÍÊÇÖ¸Ïòµ±Ç°¶ÔÏóʵÀýµÄÖ¸Õ룬²»Ö¸ÏòÈκÎÆäËû¶ÔÏó»òÀà¡£
¶þ¡¢PHPÖÐthis¡¢self¡¢parentµÄÇø±ðÖ®¶þ
´ËƪÎÒÃǾÍselfµÄÓ÷¨½øÐн²½â
Ê×ÏÈÎÒÃÇÒªÃ÷È·Ò»µã£¬selfÊÇÖ¸ÏòÀà±¾Éí£¬Ò²¾ÍÊÇselfÊDz»Ö¸ÏòÈκÎÒѾʵÀý»¯µÄ¶ÔÏó£¬Ò»°ãselfʹÓÃÀ´Ö¸ÏòÀàÖеľ²Ì¬±äÁ¿¡£¼ÙÈçÎÒÃÇʹÓÃÀà ÀïÃ澲̬£¨Ò»°ãÓùؼü×Östatic£©µÄ³ÉÔ±£¬ÎÒÃÇÒ²±ØÐëʹÓÃselfÀ´µ÷Ó᣻¹Òª×¢ÒâʹÓÃselfÀ´µ÷Óþ²Ì¬±äÁ¿±ØÐëʹÓà :: (ÓòÔËËã·ûºÅ)£¬¼ûʵÀý¡£
<?php //¶¨ÒåÒ»¸öcounterµÄÀà class counter { //¶¨ÒåÊôÐÔ£¬°üÀ¨Ò»¸ö¾²Ì¬±äÁ¿$firstCount£¬²¢¸³³õÖµ0 Óï¾ä¢Ù private static $firstCount = 0; private $lastCount; //¹¹Ô캯Êý function __construct() { $this->lastCount = ++self::$firstCount; //ʹÓÃselfÀ´µ÷Óþ²Ì¬±äÁ¿ Óï¾ä¢Ú } //´òÓ¡lastCountÊýÖµ function printLastCount() { print( $this->lastCount ); } } //ʵÀý»¯¶ÔÏó $obj = new Counter(); $obj->printLastCount(); //Ö´Ðе½ÕâÀïµÄʱºò£¬³ÌÐòÊä³ö 1 ?>ÕâÀïҪעÒâÁ½¸öµØ·½Óï¾ä¢ÙºÍÓï¾ä¢Ú¡£ÎÒÃÇÔÚÓï¾ä¢Ù¶¨ÒåÁËÒ»¸ö¾²Ì¬±äÁ¿$firstCount£¬ÄÇôÔÚÓï¾ä¢ÚµÄʱºòʹÓÃÁËselfµ÷ÓÃÕâ¸öÖµ£¬ÄÇôÕâ ʱºòÎÒÃǵ÷ÓõľÍÊÇÀà×Ô¼º¶¨ÒåµÄ¾²Ì¬±äÁ¿$frestCount¡£ÎÒÃǵľ²Ì¬±äÁ¿ÓëÏÂÃæ¶ÔÏóµÄʵÀýÎ޹أ¬ËüÖ»ÊǸúÀàÓйأ¬ÄÇôÎÒµ÷ÓÃÀà±¾ÉíµÄµÄ£¬ÄÇôÎÒÃÇ ¾ÍÎÞ·¨Ê¹ÓÃthisÀ´ÒýÓã¬ÒòΪselfÊÇÖ¸ÏòÀà±¾Éí£¬ÓëÈκζÔÏóʵÀýÎ޹ء£È»ºóÇ°ÃæʹÓõÄthisµ÷ÓõÄÊÇʵÀý»¯µÄ¶ÔÏó$obj£¬´ó¼Ò²»Òª»ìÏýÁË¡£
¹ØÓÚself¾Í˵µ½ÕâÀ½áºÏÀý×Ó»¹ÊDZȽϷ½±ãÀí½âµÄ¡£
Èý¡¢PHPÖÐthis¡¢self¡¢parentµÄÇø±ðÖ®Èý
´ËƪÎÒÃǾÍparentµÄÓ÷¨½øÐн²½â¡£
Ê×ÏÈ£¬ÎÒÃÇÃ÷È·£¬parentÊÇÖ¸Ïò¸¸ÀàµÄÖ¸Õ룬һ°ãÎÒÃÇʹÓÃparentÀ´µ÷Óø¸ÀàµÄ¹¹Ô캯Êý¡£ÊµÀýÈçÏ£º
<?php //½¨Á¢»ùÀàAnimal class Animal { public $name; //»ùÀàµÄÊôÐÔ£¬Ãû×Ö$name //»ùÀàµÄ¹¹Ô캯Êý£¬³õʼ»¯¸³Öµ public function __construct( $name ) { $this->name = $name; } } //¶¨ÒåÅÉÉúÀàPerson ¼Ì³Ð×ÔAnimalÀà class Person extends Animal { public $personSex; //¶ÔÓÚÅÉÉúÀ࣬ж¨ÒåÁËÊôÐÔ$personSexÐÔ±ð¡¢$personAgeÄêÁä public $personAge; //ÅÉÉúÀàµÄ¹¹Ô캯Êý function __construct( $personSex, $personAge ) { parent::__construct( "PBPHome" ); //ʹÓÃparentµ÷ÓÃÁ˸¸ÀàµÄ¹¹Ô캯Êý Óï¾ä¢Ù $this->personSex = $personSex; $this->personAge = $personAge; } //ÅÉÉúÀàµÄ³ÉÔ±º¯Êý£¬ÓÃÓÚ´òÓ¡£¬¸ñʽ£ºÃû×Ö is name,age is ÄêÁä function printPerson() { print( $this->name. " is " .$this->personSex. ",age is " .$this->personAge ); } } //ʵÀý»¯Person¶ÔÏó $personObject = new Person( "male", "21¡å); //Ö´ÐдòÓ¡ $personObject->printPerson(); //Êä³ö½á¹û£ºPBPHome is male,age is 21 ?>
ÀïÃæͬÑùº¬ÓÐthisµÄÓ÷¨£¬´ó¼Ò×Ô¼º·ÖÎö¡£ÎÒÃÇ×¢ÒâÕâô¸öϸ½Ú£º³ÉÔ±ÊôÐÔ¶¼ÊÇpublic£¨¹«ÓÐÊôÐԺͷ½·¨£¬ÀàÄÚ²¿ºÍÍⲿµÄ´úÂë¾ù¿É·ÃÎÊ£©µÄ£¬ÌØ ±ðÊǸ¸ÀàµÄ£¬ÕâÊÇΪÁ˹©¼Ì³ÐÀàͨ¹ýthisÀ´·ÃÎÊ¡£¹Ø¼üµãÔÚÓï¾ä¢Ù£º parent::__construct( ¡°heiyeluren¡± )£¬ÕâʱºòÎÒÃǾÍʹÓÃparentÀ´µ÷Óø¸ÀàµÄ¹¹Ô캯Êý½øÐжԸ¸ÀàµÄ³õʼ»¯£¬ÕâÑù£¬¼Ì³ÐÀàµÄ¶ÔÏó¾Í¶¼¸ø¸³ÖµÁËnameΪPBPHome¡£ÎÒÃÇ¿ÉÒÔ²âÊÔÏ£¬ ÔÙʵÀý»¯Ò»¸ö¶ÔÏó$personObject1£¬Ö´ÐдòÓ¡ºónameÈÔÈ»ÊÇPBPHome¡£
×ܽ᣺thisÊÇÖ¸Ïò¶ÔÏóʵÀýµÄÒ»¸öÖ¸Õ룬ÔÚʵÀý»¯µÄʱºòÀ´È·¶¨Ö¸Ïò£»selfÊǶÔÀà±¾ÉíµÄÒ»¸öÒýÓã¬Ò»°ãÓÃÀ´Ö¸ÏòÀàÖеľ²Ì¬±äÁ¿£»parentÊǶԸ¸ÀàµÄÒýÓã¬Ò»°ãʹÓÃparentÀ´µ÷Óø¸ÀàµÄ¹¹Ô캯Êý¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ