When you declare a class, you need to list all the variables that the objects should have - referred to as attributes and methods. 3.1.1 shows a class configuration. Note that you can only in the brace ({}) Declare variables or functions. 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
// Define a tracked user class 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 time Function getLastLogin () {Return (Date ("M D Y", $ this-> LastLogin));}} // Create an object of an object $ user = new user ("leon", "sdf123"); // Get the last time print ($ user-> getLastLogin (). "n"); / / Print the username Print ("$ user-> namen");?>
When you declare attributes, you don't need to specify data types. Variables may be integer, strings, or another object, depending on the actual situation. Increase the comment when declaring properties, is a good idea, mark the meaning of the property Type. When you declare a method, what you do and the external definition of a function is the same. Methods and properties have their own namespaces. This means you can safely build a method of the same name with the class externally, Both can't conflict. For example, a class can define a method called Date (). But you can't name a method for php keywords, such as for or while. Category method may contain called Type Hint in PHP . Type Hint is another name of the class that passes the parameter to the method. If your script calls the method and pass a variable that is not a class instance, PHP will generate a "Fatal error". You may not give other types Out of Type Hint, like integer, string, or Boolean. When writing, Type Hint should include an array type still there is a controversial .Type Hint is a shortcut for the data type of the test function parameter or operator. You It may always return this method. Confirm that you enforce a parameter which data type must be, such as integer. 3.2.1 Make sure the compiled class only generates a Widget instance. 3.2.1
// Component Class Widget {public $ name = 'none'; public $ created = false;} // assembler class assemer {public function make (widget $ w) {print ("MAKING $ W-> Namen"); $ W-> created = true;}} // Create a component object $ thing = new widget; $ think-> name = 'gadget'; // assembly component assembler :: make ($ think);?>