第三节--定义一个类
+-------------------------------------------------------------------------------+
| = 本文为Haohappy读>
| = 中Classes and Objects一章的笔记
| = 翻译为主+个人心得
| = 为避免可能发生的不必要的麻烦请勿转载,谢谢
| = 欢迎批评指正,希望和所有PHP爱好者共同进步!
| = PHP5研究中心: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/
第三节--定义一个类
当你声明一个类,你需要列出对象应有的所有变量和所有函数—被称为属性和方法. 3.1.1中显示了一个类的构成. 注意在大括号({})内你只能声明变量或者函数. 3.1.2中显示了如何在一个类中定义三个属性和两个方法.
3.1.1
复制代码 代码如下:
class Name extends Another Class
{
Access Variable Declaration
Access Function Declaration
}
3.1.2
复制代码 代码如下:
//定义一个跟踪用户的类
class User
{
//属性
public $name;
private $password, $lastLogin;
//方法
public function __construct($name, $password)
{
$this->name = $name;
$this->password = $password;
$this->lastLogin = time();
$this->accesses++;
}
// 获取最后访问的时间
function getLastLogin()
{
return(date("M d Y", $this->lastLogin));
}
}
//创建一个对象的实例
$user = new User("Leon", "sdf123");
//获取最后访问的时间
print($user->getLastLogin() ."
\n");
//打印用户名
print("$user->name
\n");
?>
当你声明属性,你不需要指明数据类型. 变量可能是整型,字符串或者是另一个对象,这取决于实际情况.在声明属性时增加注释是一个好主意,标记上属性的含义和数据类型.
当你声明一个方法,你所做的和在类外部定义一个函数是一样的. 方法和属性都有各自的命名空间. 这意味着你可以安全地建立一个与类外部函数同名的方法,两者不会冲突. 例如,一个类中可以定义一个名为date()的方法. 但是你不能将一个方法命名为PHP的关键字,如for或者while.
类方法可能包含PHP中所谓的type hint. Type hint 是另一个传递参数给方法的类的名字. 如果你的脚本调用方法并传递一个不是类的实例的变量,PHP将产生一个”致命(fatal)错误” . 你可能没有给其它类型给出type hint,就像整型,字符串,或者布尔值. 在书写的时候, type hint是否应当包含数组类型仍存在争议.
Type hint是测试函数参数或者运算符的实例的数据类型的捷径. 你可能总是返回这个方法. 确认你强制让一个参数必须是哪种数据类型,如整型. 3.2.1 确保编译类只产生Widget的实例.
3.2.1
复制代码 代码如下:
//组件
class Widget
{
public $name='none';
public $created=FALSE;
}
//装配器
class Assembler
{
public function make(Widget $w)
{
print("Making $w->name
\n");
$w->created=TRUE;
}
}
//建立一个组件对象
$thing = new Widget;
$thing->name = 'Gadget';
//装配组件
Assembler::make($thing);
?>
除了传递参数的变量外,方法含有一个特殊的变量. 它代表类的个别实例. 你应当用这个来指向对象的属性和其它方法.一些面向对象的语言假设一个不合格的变量提交给本地属性,但在PHP中方法的任何变量只是在方法的一定范围内. 注意在User类的构造函数中这个变量的使用(3.1.2).
PHP在属性和方法声明前定义一个访问限定语,如public,private和protected. 另外,你可以用”static”来标记一个成员. 你也可以在类中声明常量. 本章稍后会有不同访问方式的相关讨论.
你可以在一行中列出相同访问方式的几个属性,用逗号来分隔它们. 在3.1.2中,User类有两个private属性--$password和$lastLogin.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

"Exploring Discuz: Definition, Functions and Code Examples" With the rapid development of the Internet, community forums have become an important platform for people to obtain information and exchange opinions. Among the many community forum systems, Discuz, as a well-known open source forum software in China, is favored by the majority of website developers and administrators. So, what is Discuz? What functions does it have, and how can it help our website? This article will introduce Discuz in detail and attach specific code examples to help readers learn more about it.

Standby is a lock screen mode that activates when the iPhone is plugged into the charger and oriented in horizontal (or landscape) orientation. It consists of three different screens, one of which is displayed full screen time. Read on to learn how to change the style of your clock. StandBy's third screen displays times and dates in various themes that you can swipe vertically. Some themes also display additional information, such as temperature or next alarm. If you hold down any clock, you can switch between different themes, including Digital, Analog, World, Solar, and Floating. Float displays the time in large bubble numbers in customizable colors, Solar has a more standard font with a sun flare design in different colors, and World displays the world by highlighting

Want to make the front page of your school project look exciting? Nothing makes it stand out from other submissions like a nice, elegant border on the homepage of your workbook. However, the standard single-line borders in Microsoft Word have become very obvious and boring. Therefore, we show you the steps to create and use custom borders in Microsoft Word documents. How to Make Custom Borders in Microsoft Word Creating custom borders is very easy. However, you will need a boundary. Step 1 – Download Custom Borders There are tons of free borders on the internet. We have downloaded a border like this. Step 1 – Search the Internet for custom borders. Alternatively, you can go to clipping

The composite primary key in MySQL refers to the primary key composed of multiple fields in the table, which is used to uniquely identify each record. Unlike a single primary key, a composite primary key is formed by combining the values of multiple fields. When creating a table, you can define a composite primary key by specifying multiple fields as primary keys. In order to demonstrate the definition and function of composite primary keys, we first create a table named users, which contains three fields: id, username and email, where id is an auto-incrementing primary key and user

PHP error: Unable to declare class repeatedly, solution! It is common for developers to encounter problems. In PHP development, we often encounter a common error: the class cannot be declared repeatedly. This problem seems simple, but if not solved in time, the code will not execute correctly. This article will introduce the cause of this problem and provide a solution for your reference. When we define a class in PHP code, if the same class is defined multiple times in the same file or multiple files, an error that the class cannot be declared repeatedly will occur. This is

Naming conventions in PHP: How to use camelCase notation to name classes, methods, and variables In PHP programming, good naming conventions are an important coding practice. It improves code readability and maintainability, and makes teamwork smoother. In this article, we will explore a common naming convention: camelCase and provide some examples of how to use it in PHP to name classes, methods, and variables. 1. What is camel case nomenclature? CamelCase is a common naming convention in which the first letter of each word is capitalized,

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,
