µ±Ç°Î»ÖãºLinux½Ì³Ì - Php - µÚ1´ÎÇ×ÃܽӴ¥PHP5(1)

µÚ1´ÎÇ×ÃܽӴ¥PHP5(1)

ÎÄÕÂÀ´Ô´£ºPHPBuilder.com
Ô­×÷ÕߣºLuis Argerich
·­Ò룺erquan
erquan×¢£º±¾ÈËÏÖ»¹Î´À´µÃ¼°ÌåÑéPHP5£¬Ö»ÊÇ·­ÒëһƪÀÏÍâµÄÎÄÕ¡£
ÒÔϾùÓÉerquan·­Ò룬µÚ1´Î×÷ÕâЩµÄÊÂÇéÏ£ÍûûÓÐÎóµ¼´ó¼Ò¡£ÓÐЩ²»×¼µÄµØ·½ÇëÁ½⡣
´ó¼Ò¿´ÕâÑùµÄÐв»ÐУ¬Èç¹ûÐеĻ°£¬Å¼¾Í·­ÒëÍ꣬²»Ðоͷ­ÒëÁË£¬ÃâµÃÎóµ¼ÁË´ó¼Ò£¬Ò²ÀÛŶ¡£¡£¡£¡£:)
תÌùʱÇë×¢Ã÷ÎÄÕÂÀ´Ô´£¬Ð»Ð»£º£©


PHP5µÄÕýʽ°æ»¹Ã»·¢²¼£¬µ«ÎÒÃÇ¿ÉÒÔѧϰ¡¢ÌåÑéÏ¿ª·¢°æ¸øÎÒÃÇ´øÀ´µÄPHPÐÂÌØÐÔ¡£
±¾ÎĽ«¼¯ÖнéÉÜÒÔÏÂ3´óPHP5й¦ÄÜ£º
* жÔÏóģʽ
* ½á¹¹»¯Òì³£´¦Àí
* Ãû³Æ¿Õ¼ä

ÔÚÕýʽ¿ªÊ¼Ö®Ç°£¬Çë×¢Ò⣺
*ÎÄÕÂÖеIJ¿·ÖÀý×ÓÓÃPHP4µÄ·½·¨ÊµÏÖ£¬Ö»ÊÇΪÁËÔöÇ¿ÎÄÕµĿɶÁÐÔ
*±¾ÎÄËùÃèÊöµÄÐÂÌØÐÔ¿ÉÄÜ»áÓëÕýʽ°æÌØÐÔÓгöÈ룬ÇëÒÔÕýʽ°æ±¾Îª×¼¡£

* жÔÏóģʽ



PHP5еĶÔÏóģʽÔÚPHP4µÄ»ù´¡ÉÏ×öÁ˺ܴóµÄ"Éý¼¶"£¬Äã¿´ÆðÀ´»áºÜÏñJAVA£º£¨¡£
ÏÂÃæµÄһЩÎÄ×Ö½«¶ÔËü×öһЩ¼òµ¥½éÉÜ£¬²¢ÇÒ¸½ÓÐСÀý×ÓÈÃÄú¿ªÊ¼ÌåÑéPHP5µÄÐÂÌØÐÔ
come on~~£º£©

* ¹¹Ô캯Êý ºÍ Îö¹¹º¯Êý
* ¶ÔÏóµÄÒýÓÃ
* ¿Ë¡¶ÔÏó
* ¶ÔÏóµÄ3ÖÖģʽ£ºË½ÓС¢¹«¹²ºÍÊܱ£»¤
* ½Ó¿Ú
* ÐéÄâÀà
* __call()
* __set()ºÍ__get()
* ¾²Ì¬³ÉÔ±

¹¹Ô캯Êý ºÍ Îö¹¹º¯Êý



ÔÚPHP4ÖУ¬ºÍÀàÃûÒ»ÑùµÄº¯Êý±»Ä¬ÈÏΪ¸ÃÀàµÄ¹¹ÔìÆ÷£¬²¢ÇÒÔÚPHP4ûÓÐÎö¹¹º¯ÊýµÄ¸ÅÄî¡£(¶þȪ ×¢£ºÕâµãºÍJAVAÒ»Ñù)
µ«´ÓPHP5¿ªÊ¼£¬¹¹Ô캯Êý±»Í³Ò»ÃüÃûΪ __construct£¬¶øÇÒÓÐÁËÎö¹¹º¯Êý£º__destruct(¶þȪ ×¢£ºÕâµãÈ´ºÍDelphiÒ»Ñù£¬¿É¼ûPHP5ÎüÊÕÁËÖÚ¶àµÄ³ÉÊìµÄOO˼Ï룬¿ÉC¿ÉºØ~~):
Àý1£º¹¹Ô캯ÊýºÍÎö¹¹º¯Êý

<?php
class foo {
  var $x;

  function __construct($x) {
    $this->x = $x;
  }

  function display() {
    print($this->x);
  }

  function __destruct() {
    print("bye bye");
  }
}

$o1 = new foo(4);
$o1->display();
?>


ÔËÐÐÍêÄ㽫¿´µ½Êä³öÁË"bye bye"£¬ÕâÊÇÒòΪÀàÔÚÖÕÖ¹µÄʱºòµ÷ÓÃÁË__destruct()Îö¹¹º¯Êý~~

¶ÔÏóµÄÒýÓÃ



ÕýÈçÄãËùÖªµÀµÄÒ»Ñù£¬ÔÚPHP4ÖУ¬¶ÔÒ»¸öº¯Êý»ò·½·¨´«µÝÒ»¸ö±äÁ¿Ê±£¬Êµ¼ÊÉÏÊÇ´«µÝÁËÒ»¸öcopy£¬³ý·ÇÄãÓÃÁË´«Ö··û&À´ÉùÃ÷
ÄãÔÚ×öÒ»¸ö±äÁ¿µÄÒýÓá£ÔÚPHP5ÖУ¬¶ÔÏó×ÜÊÇÒÔÒýÓõķ½Ê½±»Ö¸¶¨£º
Àý2£º¶ÔÏóµÄÒýÓÃ

<?php
class foo {
  var $x;

  function setX($x) {
    $this->x = $x;
  }

  function getX() {
    return $this->x;
  }
}

$o1 = new foo;
$o1->setX(4);
$o2 = $o1;
$o1->setX(5);
if($o1->getX() == $o2->getX()) print("Oh my god!");
?>

(¶þȪ ×¢£ºÄ㽫¿´µ½"Oh my god!"µÄÊä³ö)

¿Ë¡¶ÔÏó



ÈçÉÏ£¬Èç¹ûÓÐʱ²»ÏëµÃµ½¶ÔÏóµÄÒýÓöøÏëÓÃcopyʱ£¬Ôõô°ì£¿ÔÚPHP5ÌṩµÄ __clone ·½·¨ÖÐʵÏÖ£º
Àý3£º¿Ë¡¶ÔÏó

<?php
class foo {
  var $x;

  function setX($x) {
    $this->x = $x;
  }

  function getX() {
    return $this->x;
  }
}

$o1 = new foo;
$o1->setX(4);
$o2 = $o1->__clone();
$o1->setX(5);

if($o1->getX() != $o2->getX()) print("Copies are independant");
?>

¿Ë¡¶ÔÏóµÄ·½·¨ÔÚÒѱ»Ó¦Óõ½ºÜ¶àÓïÑÔÖУ¬ËùÒÔÄã²»±Øµ£ÐÄËüµÄÐÔÄÜ£º£©¡£

Private, Public ºÍ Protected



ÔÚPHP4ÖУ¬Äã¿ÉÒÔÔÚ¶ÔÏóµÄÍâÃæ²Ù×÷ËüÈÎÒâµÄ·½·¨ºÍ±äÁ¿--ÒòΪ·½·¨ºÍ±äÁ¿Êǹ«Óõġ£ÔÚPHP5ÒýÓÃÁË3ÖÖģʽÀ´¿ØÖÆ
¶Ô±äÁ¿¡¢·½·¨µÄ¿ØÖÆȨÏÞ£ºPublic(¹«ÓõÄ)¡¢Protected(Êܱ£»¤)ºÍPrivate(˽ÓÐ)

Public:·½·¨ºÍ±äÁ¿¿ÉÒÔÔÚÈÎÒâµÄʱºò±»·ÃÎʵ½
Private:Ö»ÄÜÔÚÀàµÄÄÚ²¿±»·ÃÎÊ£¬×ÓÀàÒ²²»ÄÜ·ÃÎÊ
Protected:Ö»ÄÜÔÚÀàµÄÄÚ²¿¡¢×ÓÀàÖб»·ÃÎÊ

Àý×Ó4£ºPublic, protected and private

<?php
class foo {
  private $x;

  public function public_foo() {
    print("I'm public");
  }

  protected function protected_foo() {
    $this->private_foo(); //Ok because we are in the same class we can call private methods
    print("I'm protected");
  }

  private function private_foo() {
    $this->x = 3;
    print("I'm private");
  }
}

class foo2 extends foo {
  public function display() {
    $this->protected_foo();
    $this->public_foo();
    // $this->private_foo();  // Invalid! the function is private in the base class
  }
}

$x = new foo();
$x->public_foo();
//$x->protected_foo();  //Invalid cannot call protected methods outside the class and derived classes
//$x->private_foo();    //Invalid private methods can only be used inside the class

$x2 = new foo2();
$x2->display();
?>


Ìáʾ£º±äÁ¿×ÜÊÇ˽ÓÐÐÎʽ£¬Ö±½Ó·ÃÎÊÒ»¸ö˽ÓбäÁ¿²¢²»ÊÇÒ»¸öºÃµÄOOP˼Ï룬Ӧ¸ÃÓÃÆäËûµÄ·½·¨À´ÊµÏÖ set/get µÄ¹¦ÄÜ


½Ó¿Ú



ÕýÈçÄãÖªµÀµÄÒ»Ñù£¬ÔÚ PHP4 ÖÐʵÏּ̳еÄÓï·¨ÊÇ"class foo extends parent"¡£ÎÞÂÛÔÚPHP4 »¹ÊÇÔÚ PHP5 ÖУ¬¶¼²»Ö§³Ö¶àÖؼ̳м´Ö»ÄÜ´ÓÒ»¸öÀàÍùϼ̳С£ PHP5ÖеÄ"½Ó¿Ú"ÊÇÕâÑùµÄÒ»ÖÖÌØÊâµÄÀࣺËü²¢²»¾ßÌåʵÏÖij¸ö·½·¨£¬Ö»ÊÇÓÃÀ´¶¨Òå·½·¨µÄÃû³ÆºÍÓµÓеÄÔªËØ£¬È»ºóͨ¹ý¹Ø¼ü×Ö½«ËüÃÇÒ»ÆðÒýÓò¢ÊµÏÖ¾ßÌåµÄ¶¯×÷¡£

Example 5: ½Ó¿Ú
<?php
interface displayable {
  function display();
}

interface printable {
  function doprint();
}

class foo implements displayable,printable {
  function display() {
    // code
  }

  function doprint() {
    // code
  }
}
?>

Õâ¶Ô´úÂëµÄÔĶÁÐÔºÍÀí½âÐÔÊǷdz£ÓаïÖúµÄ:¶Áµ½¸ÃÀàʱ£¬Äã¾ÍÖªµÀfoo°üº¬Á˽ӿÚdisplayableºÍprintable£¬¶øÇÒÒ»¶¨ÓÐprint()(¶þȪ ×¢£ºÓ¦¸ÃÊÇdoprint())·½·¨ºÍdisplay()·½·¨¡£²»±ØÖªµÀËüÃÇÄÚ²¿ÊÇÈçºÎʵÏ־ͿÉÇáËɲÙ×÷ËüÃÇÖ»ÒªÄã¿´µ½fooµÄÉùÃ÷¡£

ÐéÄâÀà



ÐéÄâÀàÊÇÒ»ÖÖ²»Äܱ»ÊµÀý»¯µÄÀ࣬Ëü¿ÉÒÔÏñ³¬ÀàÒ»Ñù£¬¿ÉÒÔ¶¨Òå·½·¨ºÍ±äÁ¿¡£
ÔÚÐéÄâÀàÖл¹¿ÉÒÔ¶¨ÒåÐéÄâµÄ·½·¨£¬¶øÇÒÔڸ÷½·¨Ò²²»ÄÜÔÚ¸ÃÀàÊDZ»ÊµÏÖ£¬µ«±ØÐëÔÚÆä×ÓÀàÖб»ÊµÏÖ

Example 6: ÐéÄâÀà

<?php
abstract class foo {
  protected $x;

  abstract function display();

  function setX($x) {
    $this->x = $x;
  }
}


class foo2 extends foo {
  function display() {
    // Code
  }
}
?>


__call()·½·¨



ÔÚPHP5ʱ£¬Èç¹ûÄ㶨ÒåÁË __call()·½·¨£¬µ±ÄãÊÔͼ·ÃÎÊÀàÖÐÒ»¸ö²»´æÔڵıäÁ¿»ò·½·¨Ê±£¬__call()¾Í»á±»×Ô¶¯µ÷ÓÃ:
Example 7: __call


<?php
class foo {

  function __call($name,$arguments) {
    print("Did you call me? I'm $name!");
  }
}

$x = new foo();
$x->doStuff();
$x->fancy_stuff();
?>


Õâ¸öÌØÊâµÄ·½·¨±»Ï°¹ßÓÃÀ´ÊµÏÖ"·½·¨ÖØÔØ"£¬ÒòΪÄãÒÀ¿¿Ò»¸ö˽ÓвÎÊýÀ´ÊµÏÖ²¢¼ì²éÕâ¸ö²ÎÊý£º
Exampe 8:  __call ʵÏÖ·½·¨ÖØÔØ

<?php
class Magic {

  function __call($name,$arguments) {
    if($name=='foo') {
      if(is_int($arguments[0])) $this->foo_for_int($arguments[0]);
      if(is_string($arguments[0])) $this->foo_for_string($arguments[0]);
    }
  }

  private function foo_for_int($x) {
    print("oh an int!");
  }

  private function foo_for_string($x) {
    print("oh a string!");
  }
}

$x = new Magic();
$x->foo(3);
$x->foo("3");
?>



__set()·½·¨ ºÍ __get()·½·¨



µ±·ÃÎÊ»òÉèÖÃÒ»¸ö䶨ÒåµÄ±äÁ¿Ê±£¬ÕâÁ½¸ö·½·¨½«±»µ÷Óãº

Example 9: __set and __get

<?php
class foo {

  function __set($name,$val) {
    print("Hello, you tried to put $val in $name");
  }

  function __get($name) {
    print("Hey you asked for $name");
  }
}

$x = new foo();
$x->b