gpt4 book ai didi

PHP:类之间互相使用函数吗?

转载 作者:行者123 更新时间:2023-12-03 02:01:10 25 4
gpt4 key购买 nike

我已经陷入这个问题有一段时间了,并在 Stackoverflow 上提出了一些相关问题。我有一些建议,但我就是无法理解。有人可以给我提供一个类(class)顺利协作的例子吗?

我有 3 个(也许更多)类(class):

  • mysql
  • 用户
  • 提醒

正如我所说,有人可以提供一个示例,以便这些类可以使用彼此类中的函数,例如用户可以使用mysql的功能。我要求提供一个简单的示例,以供学习之用。

请不要使用谷歌建议或其他问题的链接。我已经尝试搜索这个有一段时间了。但没有成功。

马蒂·莱恩

最佳答案

我真的建议您先阅读有关类(class)的内容 - http://php.net/manual/en/language.oop5.php因为这些都是基本概念,如果您不理解,单个代码示例对您没有多大帮助。

class Mysql {
// Public function accessible from everywhere, with class instance
public function hello() {
echo '<br>Mysql says Hello';
}
// Static function accesible from everywhere, without class instance
static function bye() {
echo '<br>Mysql says Bye';
}
}

class User {
// Public function accessible from everywhere, with class instance
public function hello() {
$mysql = new Mysql();
$mysql->hello();
Mysql::bye();
}
}

class Alert {
// Static function accesible from everywhere, without class instance
static function hello() {
$user = new User();
$user->hello();
}
}

$user = new User();
$user->hello();

Mysql::bye();

Alert::hello();

关于PHP:类之间互相使用函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2592711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com