How to define a class _php tutorial in PHP

Source: Internet
Author: User
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 Figure 1 shows the composition of a class. Note that you can only declare variables or functions within curly braces ({}). Figure 2 shows how to define three properties and two methods in a class.

Class Name extends another class {    Access Variable Declaration    

Figure 1

     
     name = $name;            $this->password = $password;            $this->lastlogin = time ();            $this->accesses++;        }        Gets the last accessed time        function Getlastlogin ()        {            return (date ("M D Y", $this->lastlogin));        }    }    Create an instance of an object    $user = new User ("Leon", "sdf123");    Gets the last visited time    print ($user->getlastlogin ().
n "); Prints the user name print ("$user->name

Figure 2

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. Figure 3 Ensuring that the compiled class produces only instances of the widget

     n ");            $w->created=true;        }    }    Create a Component Object    $thing = new Widget;    $thing->name = ' Gadget ';    Assembly Kit    Assembler::make ($thing);

Figure 3

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 that this variable is used in the constructor of the user class in Figure 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 Figure 2, the user class has two private properties--$password and $lastlogin.


http://www.bkjia.com/PHPjc/446724.html www.bkjia.com true http://www.bkjia.com/PHPjc/446724.html techarticle 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 Figure 1 shows the composition of a class. Note that you can only declare changes within curly braces ({}) .

  • 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.