Section three-Defining a class _php tutorial

Source: Internet
Author: User
+-------------------------------------------------------------------------------+
| = This is haohappy read < >
| = Notes in Classes and objects Chapter
| = Translation-based + personal experience
| = Please do not reprint in order to avoid the unnecessary trouble that may occur.
| = Welcome criticism, hope and all the PHP enthusiasts to progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/

Section three-Defining a class

When you declare a class, you need to list all the variables and all functions that the object should have-called properties and methods. The composition of a class is shown in 3.1.1. Note You can only declare variables or functions within curly braces ({}). 3.1.2 shows how to define three properties and two methods in a class.

3.1.1

Copy CodeThe code is as follows: Class Name extends another class
{
Access Variable Declaration
Access Function Declaration
}



3.1.2

Copy CodeThe code is as follows: Define a class that tracks users
Class User
{
Property
Public $name;
Private $password, $lastLogin;

Method
Public function __construct ($name, $password)
{
$this->name = $name;
$this->password = $password;
$this->lastlogin = time ();
$this->accesses++;
}

Gets the last access time
function Getlastlogin ()
{
Return (Date ("M D Y", $this->lastlogin));
}
}

Create an instance of an object
$user = New User ("Leon", "sdf123");

Gets the last access time
Print ($user->getlastlogin (). "
\ n ");

Print User name
Print ("$user->name
\ n ");
?>


When you declare a property, you do not need to indicate the data type. A variable may be an integer, a string, or another object, depending on the actual situation. Adding a comment when declaring a property is a good idea to mark the meaning and data type of a property.

When you declare a method, what you do and define a function outside the class is the same. Both methods and properties have their own namespaces. This means that you can safely create a method with the same name as an external function of the class, which does not conflict. For example, you can define a method named Date () in a class. However, you cannot name a method as a PHP keyword, such as for or while.

The class method may contain the so-called type hint in PHP. Type hint is the name of another class that passes parameters to the method. If your script invokes a method and passes a variable that is not an instance of the class, PHP will produce a "fatal (fatal) error". You may not give type hint to other types, like integers, strings, or Boolean values. There is still controversy as to whether type hint should contain an array type at the time of writing.

Type hint is a shortcut to test the data type of a function parameter or an instance of an operator. You may always return to this method. Verify that you force a parameter to be of which data type, such as Integer. 3.2.1 Ensure that the compiled class produces only instances of the widget.

3.2.1

Copy CodeThe code is as follows: Component
Class Widget
{
Public $name = ' None ';
Public $created =false;
}

Assembly device
Class Assembler
{
Public function make (Widget $w)
{
Print ("Making $w->name
\ n ");
$w->created=true;
}
}

Building a Component Object
$thing = new Widget;
$thing->name = ' Gadget ';

Assembly components
Assembler::make ($thing);
?>




In addition to variables that pass parameters, the method contains a special variable. It represents an individual instance of a 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 a local property, but that any variable in the method in PHP is only within a certain range of the method. Note the use of this variable in the constructor of the user class (3.1.2).

PHP defines an access qualifier, such as public,private and protected, before the property and method declarations. In addition, you can use "static" to mark a member. You can also declare constants in a class. This chapter will have a discussion about different ways of accessing it later.

You can list several properties of the same access method in a row, 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 is haohappy read corephpprogramming |= in ClassesAndObjects a chapter of the notes |= translation of the main + personal heart ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.