PHP object-oriented Journey: class attributes

Source: Internet
Author: User

In PHP5, you can choose not to set the initial values in the attribute definition, or assign the following red values. In PHP, there are eight simple types: • Value Type 1. boolean Type 2. integer 3. float floating point type, also known as double floating point type 4. string • composite type 1. array 2. object • special type 1. resource 2. NULL 01 <? 02 class A {03} 04 05 class Person {06 private $ name; // The definition attribute is not assigned A value. 07 private $ name1 = NULL; // defines the attribute NULL value, which is the same as that without a value assignment. 08 private $ married = true; // assign a value to the attribute with a Boolean value. 09 private $ grade = 0; // assign 10 private $ eyesight = 0.1 to the attribute with an integer value; // assign 11 private $ nationality = "China" to the attribute with a floating point number "; // assign a value of 12 private $ arr = array ("foo" => "bar", 12 => true) to the attribute using a string ); // use an array to assign a value to the attribute 13 // private $ A = new a (); // PHP5 is not allowed, assign the object type to attribute 14 // private $ res = opendir ("abc "); // PHP5 does not allow Resource Type 15 // private $ g = $ this-> grade; // The attribute previously defined is not allowed to assign values to the new attribute. 16 17} 18 $ a = new Person (); 19?> In the preceding example, Row 3 shows an error when you try to create an object and assign the value to attribute $. Row 3: an error occurs when a resource is created and copied to $ res. Row 3, using the property defined above to assign values to a new property also produces errors. In Java, you can perform operations such as 13 rows and 15 rows. The default value of the property defined in PHP5 is limited to the simplest way. Other operations are handed over to the constructor operation. The constructor will be explained later. The value transfer method between a variable and a common variable is the value assignment. For example, array. 1 <? 2 $ arr = array ("foo" => "bar", 12 => true); 3 $ a = $ arr; 4 5 $ arr [foo] = "new "; 6 print_r ($ arr); 7 echo '<br/>'; 8 print_r ($ a); 9?> Program output: view sourceprint? 1 Array ([foo] => new [12] => 1) 2 Array ([foo] => bar [12] => 1) pointing to the object's variable, is a reference variable. In this variable, the memory address of the object is stored. When you reference a variable to pass a value, the object's point is passed. Instead of copying this object. Attribute extension $ this refers to the current object. $ This-> call the attributes or methods of the current object. When $ this-> is used in a class to call an undefined property, PHP5 automatically creates an attribute for use. The default permission for this created property is public. 01 <? 02 class A {03 public $ name = "Gonn"; 04 05 public function _ construct () {06 $ this-> age = "24 "; 07} 08} 09 10 $ p = new A (); 11 12 echo $ p-> name; 13 echo '<br/>'; 14 echo $ p-> age; 15?> Program output: The 1 Gonn 2 24 attribute age is created.

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.