Section III-Defining a class--Classes and Objects in PHP5 [3]

Source: Internet
Author: User
Tags date define functions variables php programming variable access
OBJECT|PHP5/*
+-------------------------------------------------------------------------------+
| = This article is for Haohappy read <<core PHP programming>>
| = Notes from the chapter classes and objects
| = translation-oriented + personal experience
| = Please do not reprint to avoid any unnecessary trouble that may occur, thank you
| = Welcome to criticize, hope and all PHP enthusiasts to progress together!
+-------------------------------------------------------------------------------+
*/

Section III-Defining a class

When you declare a class, you need to list all the variables and all the functions that the object should have-called properties and methods. 3.1.1 shows the composition of a class. Note that 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
Class Name extends Another class
{
Access Variable Declaration
Access Function Declaration
}



3.1.2
<?php
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++;
}

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

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

Get the last access time
Print ($user->getlastlogin (). " <br>n ");

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



When you declare attributes, you do not need to specify the data type. The variable may be an integral type, a string, or another object, depending on the actual situation. Adding comments when declaring a property is a good idea to mark the meaning of the attribute and the data type.

When you declare a method, what you do is the same as defining a function outside of the class. Both methods and properties have their own namespaces. This means that you can safely create a method with the same name as the outer function of the class, which does not conflict. For example, you can define a method named Date () in a class. But you can't name a method as a keyword in PHP, 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 arguments 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, just like integers, strings, or Boolean values. It is debatable whether type hint should contain the array type when writing.

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

3.2.1
<?php
Component
Class Widget
{
Public $name = ' None ';
Public $created =false;
}

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

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

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




The method contains a special variable in addition to the variable that passes the argument. 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 attribute, but 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. Alternatively, you can use "static" to mark a member. You can also declare constants in a class. Later in this chapter, we will discuss the different ways of accessing.

You can list several properties of the same access method on one line, separating them with commas. In 3.1.2, the user class has two private properties-$password and $lastlogin.

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.