1

こんにちは、私は PHP が初めてで、PHP クラスの基本を理解するために助けが必要です。

private public protected と static を使用するクラスの例が必要です。
そして、それらはどのように機能しますか..

前もって感謝します。

ああ、私も拡張する方法を忘れていました。私は親子について何かを話している..ありがとう。

4

4 に答える 4

6

Googleにはたくさんの例があります

于 2010-10-07T14:28:38.407 に答える
2

必要なもの: http://php.net/manual/en/language.oop5.php !

于 2010-10-07T14:27:45.430 に答える
2

http://www.expertrating.com/courseware/PHPcourse/PHP-Classes-Objects-6.asp

于 2010-10-07T14:45:25.403 に答える
1

絶対に他の人に同意します。他の回答で提供されているリンクを含む、5 億 5000 万のオンライン PHP マニュアルを読む必要があります。その間、次のようになります。

class one {
   private $name;
   const ONE = 'ONE';

   // php magic function.  allocate memory for object upon instantiation
   // called with new
   public function __construct($name = null) {
      $this->init($name);
   }

   protected function name() {
      return $this->name;
   }

   // php magic function called when object variable is accessed in a string context
   public function __toString() {
      return __CLASS__ . ': ' . $this->name;
   }

   // static functions can be called without instantiation
   public static function con() {
      echo self::ONE;
   }

   private function init($name) {
      $this->name = $name;
   }
}


// two cannot overwrite method init() -- it is private.
// two has no access to $name.  It is private in one and not visible to two
class two extends one {
   // protected methods can be overwritten
   protected function name() {
      return parent::name();
   }
   // so can public methods
   public function __toString() {
      return __CLASS__ . ': ' . $this->name();
   }
}

// static method call; no instantiation needed
echo one::con() . "\n"; // ONE
// public methods can be called by child class
echo two::con() . "\n"; // ONE
$one = new one('uno');
echo "$one\n"; // one: uno
$two = new two('dos');
echo "$two\n"; // two: dos
$one->name(); // bork! No public access to this method
于 2010-10-08T05:41:41.927 に答える