PHP:静态函数的使用

发布于 2024-10-09 08:38:29 字数 646 浏览 5 评论 0原文

我有一个愚蠢的问题。 我一直在尝试编写我的框架,但是......我错过了一些东西。 我有一个基类 Base.class.php,具有一些功能。 当我编写另一个扩展 Base 的类 SubBase.class.php 时,我尝试设置一个必须在 Base 类中使用的变量,在一个静态函数中(可能)。 类似的东西

class Base { 
 public $vars;
 public function GetA() { 
  return $this->vars;
 }
}


public SubBase extends Base { 
 public function __construct() { 
  $this->vars = array();
 }
}

但是,我错过了一些东西...... 在角色中,我的 SubBase 是我的网站的子页面,Base 是 printHTML 类。我试图用我的 Base 类为我的网站设置一个标题,他在我的 SubBase 类 oO 中设置了 $this->vars 如果我错了,请告诉我,并让我知道如何写这个。 我只想写下来

<title> <?php echo Base::GetTitle(); ?> </title>

并展示出来。

i've one stupid question.
I keep trying to write my framework, but ... i miss something.
I have one base class Base.class.php, with some functions.
When, i write another class SubBase.class.php, who extends Base, i trying to set one variable , who must use in Base class, in one static function (maybe).
Something like that

class Base { 
 public $vars;
 public function GetA() { 
  return $this->vars;
 }
}


public SubBase extends Base { 
 public function __construct() { 
  $this->vars = array();
 }
}

But, i missing something ...
In role, my SubBase is subpage from my web, Base is printHTML class. I trying to set a title to my web, with my Base class, who set $this->vars in my SubBase class o.O
Please, tell me if i'm wrong, and let me know how to write this.
I wonna just write

<title> <?php echo Base::GetTitle(); ?> </title>

and show in.

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

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

发布评论

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

评论(1

む无字情书 2024-10-16 08:38:29

好吧,您应该看一下静态属性和方法:http://php. net/manual/en/language.oop5.static.php

为了完成你想要的,你可以尝试以下操作:

class Base { 

   //--------------------------------
   // Declare static property
   public static $title = '';

   //--------------------------------
   // Declare static method
   public static function GetTitle() { 
      return self::$title;
   }
}

public SubBase extends Base { 

   //--------------------------------
   // Construct which overwrites
   // Base::$title
   public function __construct($newTitle){
      self::$title = $newTitle;
   }
}

//--------------------------------
// Instantiate your SubBase object
$subPage = new SubBase($newTitle = 'Welcome to my sub page');

//--------------------------------
// And in your HTML, use
<title> <?php echo SubBase::GetTitle(); ?> </title>

注意,我使用了 SubBase::GetTitle();而不是 Base::GetTitle();如果您使用 Base::GetTitle(),您的输出将为空白,因为您正在使用基类中给定的值。在我的示例中,其:

public static $title = '';

但是,当您实例化 SubBase 类时,您提供一个“$newTitle”参数,然后该参数将覆盖空白值。

理想情况下,这应该可行。不过,我建议您更多地了解静态属性和方法的使用。

希望这有帮助。

Well, you should take a look at static properties and methods: http://php.net/manual/en/language.oop5.static.php

To accomplish what you want, you can try the following:

class Base { 

   //--------------------------------
   // Declare static property
   public static $title = '';

   //--------------------------------
   // Declare static method
   public static function GetTitle() { 
      return self::$title;
   }
}

public SubBase extends Base { 

   //--------------------------------
   // Construct which overwrites
   // Base::$title
   public function __construct($newTitle){
      self::$title = $newTitle;
   }
}

//--------------------------------
// Instantiate your SubBase object
$subPage = new SubBase($newTitle = 'Welcome to my sub page');

//--------------------------------
// And in your HTML, use
<title> <?php echo SubBase::GetTitle(); ?> </title>

Note, that I used SubBase::GetTitle(); and not Base::GetTitle(); If you use Base::GetTitle(), you're output will be blank because you're using the value given at the Base Class. In my example, its:

public static $title = '';

However, when you instantiate your SubBase class, you provide a "$newTitle" parameter, which then overrides the blank value.

Ideally, this should work. Still, I recommend you learn more about the use of static properties and methods.

Hope this helps.

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