当前位置:网站首页>「PHP基础知识」PHP中对象的使用
「PHP基础知识」PHP中对象的使用
2022-08-02 03:16:00 【夜晚回家】
功能要求
定义一个Animal类,其中包括$name、$age、$weight、$sex属性和run()和eat()方法,在run()方法中输出“Haha. I can run!”,在eat()方法中输出“Haha. I can eat!”。创建一个Animal类的对象,并分别调用run()和eat()方法将两个方法中的输出语句的信息显示在页面中。
实例代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中对象的使用</title>
</head>
<body>
<?php
class Animal
{
public $name; //动物名称
public $age; //动物年龄
public $weight; //动物体重
public $sex; //动物性别
public function run()//方法:会跑
{
echo "Haha. I can run!<br />";
}
public function eat()//方法:能吃
{
echo "Haha. I can eat!<br />";
}
}
$dog = new Animal();
$dog->run();//输出:Haha. I can run!
$dog->eat();//输出:Haha. I can eat!
?>
</body>运行结果
知识说明
目前编程语言用到的方法有面向过程和面向对象两种。在PHP中,用户可以使用者两种方法来调用方法。
对象是一种高级的数据类型。任何事物都可以被看作一个对象。一个对象由部分属性值和方法构成,属性表明对象的一种状态,方法通常是用来实现功能的。
边栏推荐
猜你喜欢

关于跨域问题

MySQL8 - use under Windows package installation method

JunitTest单元测试

Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers

WebShell connection tools (Chinese kitchen knife, WeBaCoo, Weevely) use

LeetCode:746. 使用最小花费爬楼梯【动态规划】

Keil development environment installation tutorial

SSM网上商城购物系统(前台+后台)

Using WebShell to get Shell Skills

DAY-1 | 求两个正整数的最大公约数与最小公倍数之和——辗转相除法
随机推荐
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
蓝鲸DevOps荣获平台类工具企业选择率第一
[Remote Control Development Basic Tutorial 3] Crazy Shell Open Source Formation UAV-ADC (Joystick Control)
PyTorch(六)——PyTorch可视化
PHP WebShell Free Kill
Freeswitch操作基本配置
JDBC--Druid数据库连接池以及Template基本用法
赶紧进来!!!教你用C语言写三子棋小游戏
5.合宙Air32F103_LCD_key
自定义mvc框架复习(crud)
CV-Model【4】:MobileNet v3
HCIP Day 11_MPLS Experiment
JSP WebSehll backdoor script
Small program (necessary common sense for development) 1
(转帖)hashcode和equals的关系
MySQL8--Windows下使用压缩包安装的方法
第十一天&shell脚本
深度学习:目标检测入门知识
【C语言万字长文】 宏定义 结构体 共用体 内存对齐知识点总结
1. 获取数据-requests.get()