使用 php 的简单类

发布于 2024-09-26 23:50:18 字数 169 浏览 1 评论 0原文

你好,我是 PHP 新手,我需要帮助来了解 PHP 类的基础知识。

我想要一个使用 private public protected 和 static 的类的示例。
以及它们是如何工作的..

提前致谢。

哦,我也忘记了如何扩展。 我在谈论父母和孩子什么的。 再次感谢。

hello I'm new to PHP and I need help to understand the basics of PHP class.

I want to have example of a class that uses private public protected and static.
and how do they work..

Thanks in advance.

Oh I forgot how to extends also.
I'm talking about the parent and child something or what..
Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温暖的光 2024-10-03 23:50:18

绝对同意其他人的观点。您应该阅读 5.5 亿份在线 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

Definitely agree with everyone else. You should read up on the 550 million online PHP manuals including the links provided in other answers. In the meantime, you get this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文