Home  >  Article  >  Backend Development  >  How to define a class in PHP_PHP Tutorial

How to define a class in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:25:001194browse

When you declare a class, you need to list all the variables and all the functions that the object should have? They are called properties and methods. Figure 1 shows the composition of a class. Note the curly brackets ({}) You can only declare variables or functions. Figure 2 shows how to define three properties and two methods in a class.

class Name extends Another Class {    Access Variable Declaration    Access Function Declaration } 

Figure 1

<?php //定义一个跟踪用户的类    class User    {        //属性        public $name;        private $password, $lastLogin;        //方法        public function __construct($name, $password)        {            $this->name = $name;            $this->password = $password;            $this->lastLogin = time();            $this->accesses++;        }        // 获取最后访问的时间        function getLastLogin()        {            return(date("M d Y", $this->lastLogin));        }    }    //创建一个对象的实例    $user = new User("Leon", "sdf123");    //获取最后访问的时间    print($user->getLastLogin() ."<br>n");    //打印用户名    print("$user->name<br>n"); ?> 

Figure 2

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 comments when declaring properties, tag The meaning and data type of the above properties.

When you declare a method, you are doing the same as defining a function outside the class. Methods and properties have their own namespaces. This means that you can It is safe to create a method with the same name as an external function of the class, so that the two will not conflict. For example, a class can define a method named date(). But you cannot name a method after a PHP keyword, such as for or while.

class methods may contain what is called a type hint in PHP. A type hint is the name of another class that passes parameters 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 hint is a shortcut for testing the data type of a function parameter or an instance of an operator. You may always return this method. Make sure you enforce what data type a parameter must be, such as integer . Figure 3 Ensure that the compiled class only produces instances of Widget

<?php    //组件    class Widget    {        public $name='none';        public $created=FALSE;    }    //装配器    class Assembler    {        public function make(Widget $w)        {            print("Making $w->name<br>n");            $w->created=TRUE;        }    }    //建立一个组件对象    $thing = new Widget;    $thing->name = 'Gadget';    //装配组件    Assembler::make($thing); ?>

Figure 3

In addition to the variables passed to the parameters, the method contains a special variable. It represents an individual instance of the class. You You should use this to point to properties and other methods of the object. Some object-oriented languages ​​assume that 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 that in the constructor of the User class The use of this variable in the function Figure 2.
PHP defines an access qualifier before the declaration of properties and methods, such as public, private and protected. In addition, you can use "static" to mark a member. You can also use it in a class Declare constants in. There will be a discussion of different access methods later in this chapter.
You can list several properties with the same access method on one line, separating them with commas. In Figure 2, the User class has two private attributes--$password and $lastLogin.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446724.htmlTechArticleWhen you declare a class, you need to list all the variables and all the functions that the object should have? They are called properties and methods Figure 1 shows the composition of a class. Note that within curly braces ({}) you can only declare variables...
Statement:
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