Section 3 - Define a class_PHP tutorial

WBOY
Release: 2016-07-21 16:00:56
Original
720 people have browsed it

+------------------------------------------------- ----------------------------------+
| = 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:
class Name extends Another Class 🎜>



Copy code

The code is as follows:

//Define a class for tracking users
class User 
                                                    construct($name, $password)
{ $this->name = $name; $this->password = $password; $this->lastLogin = time(); $this- >accesses++;                                         ", $this-> lastLogin)); } }
}

//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

Copy the code The code is as follows:
//Components
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...

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!