Home > Backend Development > PHP Tutorial > Section 3--Define a class--ClassesandObjectsinPHP53_PHP tutorial

Section 3--Define a class--ClassesandObjectsinPHP53_PHP tutorial

WBOY
Release: 2016-07-13 17:24:23
Original
939 people have browsed it

/* +-------------------------------------------------- --------------------------------+ | = This article is read by Haohappy> | = Classes and Objects 1 Chapter's notes | = Translation + personal experience | = To avoid unnecessary trouble, please do not reprint, thank you | = Criticisms and corrections are welcome, and I hope to make progress together with all PHP enthusiasts! +------- -------------------------------------------------- -----------------------+ */ Section 3 - Defining a class When you declare a class, you need to list all the variables and All functions—called properties and methods. The structure of a class is shown in 3.1.1. Note that you can only declare variables or functions within curly braces ({}). How to define them in a class is shown in 3.1.2 Three properties and two methods. 3.1.1 class Name extends Another Class { Access Variable Declaration Access Function Declaration } 3.1.2 name = $name; $this->password = $password; $this->lastLogin = time(); $this->accesses++; } // Get the time of the last access function getLastLogin() { return(date("M d Y", $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 the 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. When declaring a property It's a good idea to add comments, marking the meaning and data type of the property. When you declare a method, you are doing the same as defining a function outside the class. Methods and properties have their own namespace. This means This means that you can safely create a method with the same name as an external function of the class without the two conflicting. 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. The type hint is the name of another class that is passed as an argument to the method. If your script calls the method and passes a variable that is not an instance of the class, PHP will generate a "fatal error". You may not have given a type hint for other types, such as integers, strings, or booleans. At the time of writing, it was controversial whether the type hint should include array types. Type hints are 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 a parameter to be of a data type, such as an integer. 3.2.1 Make sure the compiled class only produces instances of Widget . 3.2.1 name
n"); $w->created=TRUE; } } //Create a component object $thing = new Widget; $thing->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 language assumptions An unqualified variable is submitted to a local property, but in PHP any variable of a method is 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 declares Define an access qualifier before, such as public, private, and protected. In addition, you can mark a member with "static". You can also declare constants in the class. There will be a discussion of different access methods 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532165.htmlTechArticle/* +--------------------- -------------------------------------------------- --------+ | = This article is read by Haohappy> | = Notes from the Chapter Classes and Objects| = Translation + personal experience...
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