Section 3 - Define a class_PHP tutorial
+------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy<
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = Criticisms are welcome Correction, hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------- -------------------------------------------------- ----------------+
*/
Section 3 - Defining a class
When you declare a class, you need to list All variables and all functions that an object should have - called properties and methods. The composition of a class is shown in 3.1.1. Note that within curly brackets ({}) you can only declare variables or functions. Shown in 3.1.2 Learn how to define three properties and two methods in a class.
3.1.1
Copy code
The code is as follows:
//Define a class for tracking users
class User
construct($name, $password)
//Create an instance of an object
$user = new User("Leon", "sdf123");
/ /Get the time of the last visit
print($user->getLastLogin() ."
n");
//Print user name
print("$user-> ;name
n");
?>
When you declare a property, you do not need to specify the data type. The variable may be an integer, a string or another object, depending on the actual situation. It is a good idea to add annotations when declaring properties The idea is to mark the meaning and data type of the properties.
When you declare a method, you do the same as defining a function outside the class. Methods and properties have their own namespaces. This means This means that you can safely create a method with the same name as an external function of the class, and the two will not conflict. For example, a class can define a method named date(). But you cannot name a method as a PHP keyword. Such as for or while.
Class methods may contain what is called a type hint in PHP. Type hint is the name of another class that passes parameters to the method. If your script calls the method and passes an instance that is not a class variables, PHP will generate a "fatal error". You may not give a type hint for other types, like integers, strings, or booleans. When writing, whether the type hint should include the array type or not Controversial.
Type hint is a shortcut for testing the data type of an instance of a function parameter or operator. You may always return this method. Make sure you enforce what data type a parameter must be, such as integer . 3.2.1 Ensure that the compiled class only produces instances of Widget.
3.2.1
class Widget
{
public $name='none';
public $created=FALSE;
} >
//Assembler
class Assembler
{
public function make(Widget $w)
{
print("Making $w->name
n") ;
$w->created=TRUE;
; name = 'Gadget';
//Assembly component
Assembler::make($thing);
?> 🎜> In addition to the variables passed to the parameters, methods contain a special variable. It represents an individual instance of the class. You should use this to point to the object's properties and other methods. Some object-oriented languages assume that an unqualified variable is submitted to the local Attributes, but in PHP any variables of a method are only within a certain scope of the method. Note the use of this variable in the constructor of the User class (3.1.2).
PHP defines attributes and methods before they are declared An access qualifier, such as public, private, and protected. In addition, you can mark a member with "static". You can also declare constants in the class. Different access methods are discussed later in this chapter.
You can list several properties with the same access method in one line, separating them with commas. In 3.1.2, the User class has two private properties-$password and $lastLogin.
http://www.bkjia.com/PHPjc/316958.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/316958.html
TechArticle
+----------------------- -------------------------------------------------- ------+ |=This article is Haohappy's notes from the chapter ClassesandObjects when reading CorePHP Programming |=Translated mainly + personal thoughts...

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



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

"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.

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

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

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

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

What are full-width characters? In computer encoding systems, double-width characters are a character encoding method that takes up two standard character positions. Correspondingly, the character encoding method that occupies a standard character position is called a half-width character. Full-width characters are usually used for input, display and printing of Chinese, Japanese, Korean and other Asian characters. In Chinese input methods and text editing, the usage scenarios of full-width characters and half-width characters are different. Use of full-width characters Chinese input method: In the Chinese input method, full-width characters are usually used to input Chinese characters, such as Chinese characters, symbols, etc.
