Home > Backend Development > PHP Tutorial > PHP Object-Oriented Journey: Properties of Classes_PHP Tutorial

PHP Object-Oriented Journey: Properties of Classes_PHP Tutorial

WBOY
Release: 2016-07-14 10:08:42
Original
922 people have browsed it

In PHP5, you can not set an initial value in the attribute definition, or assign the following red type initial value.

There are 8 simple types in PHP, namely:
•Numeric type
1.boolean Boolean type
2.integer integer
3.float floating point type, also known as double double precision floating point type
4.string string
•Composite type
1.array array
2.object object
•Special types
1.resource 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; //Define attribute null value, which is the same as no value assigned.
08 private $married = true; //Use Boolean to assign values ​​to attributes.
09 private $grade = 0; //Use integer values ​​to assign values ​​to attributes
10 private $eyesight = 0.1; //Use floating point numbers to assign values ​​to attributes
11 private $nationality = "China"; //Use string to assign attribute value
12 private $arr = array("foo" => "bar", 12 => true); // Use array to assign values ​​to attributes
13 //private $a = new A(); //PHP5 does not allow, create object type and assign value to attribute
14 //private $res = opendir("abc"); // PHP5 does not allow the use of resource types
15 //private $g = $this->grade; //Assigning values ​​to new properties using previously defined properties is not allowed.
16
17 }
18 $a = new Person();
19 ?>
In the above example, line 13, trying to create an object and assign a value to property $a will result in an error.
On line 14, an error will occur when creating the resource and copying it to $res.
Line 15, assigning a value to a new attribute using the attribute defined above will also generate an error.
In Java, you can do operations like lines 13 and 15. Defining default values ​​for properties in PHP5 is restricted to the simplest way. Other operations are left to the construction method. The construction method will be explained in the following content.
Variables and reference variables
The value transfer method between ordinary variables is value assignment. Such as arrays.
1
2 $arr = array("foo" => "bar", 12 => true);
3 $a = $arr;
4
5 $arr[foo] = "new";
6 print_r($arr);
7 echo '
';
8 print_r($a);
9 ?>
Program output:
view sourceprint?
1 Array ( [foo] => new [12] => 1 )
2 Array ( [foo] => bar [12] => 1 )
The variable pointing to the object is a reference variable. Stored in this variable is the memory address of the pointed object. When passing a value by reference to a variable, what is passed is the pointer to the object. rather than copying the object.
Expansion of attributes
$this refers to the current object.
$this-> Call the properties or methods of the current object.
When using $this-> in a class to call an undefined property, PHP5 will automatically create a property for use.
The default method permission of this created attribute 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 '
';
14 echo $p->age;
15 ?>
Program output:
1 Gonn
2 24
The attribute age is created.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477753.htmlTechArticleIn PHP5, you can not set an initial value in the attribute definition, or assign the following red type initial value. There are 8 simple types in PHP, which are: Numeric type 1.boolean Boolean type 2.integer...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template