Members of php object-oriented. In-depth knowledge of php object-oriented. Is php object-oriented? php object-oriented and more

WBOY
Release: 2016-07-29 08:53:27
Original
1065 people have browsed it

Members of the

class:
1. Constant: const constant name = value;

2. Attribute
2.1 Static properties
2.2 Instance attributes

3. Methods
3.1 Static methods
3.2 Instance methods
3.3 Construction method
3.4 Destruction method

Note:
①Static attributes belong to the class, and objects created through this class share this attribute
②Static properties and static methods can be used in instance methods
③Instance attributes and instance methods cannot be accessed in static methods, because static ones belong to classes, and instance ones belong to objects. When the class exists, the object has not been new yet!

<code><span><span><?php</span>header(<span>"content-type:text/html;charset=utf-8"</span>);

<span><span>class</span><span>C</span>{</span><span>const</span> PATH = <span>"C:/windows"</span>;<span>//类中不能使用 define定义常量</span><span>//静态属性</span><span>static</span><span>$count</span>  = <span>66</span>;<span>//等价于 static public $count = 66;</span><span>//实例属性</span><span>public</span><span>$name</span> = <span>"tom"</span>;<span>//等价于 var $name = "tom";</span><span>//默认的无参构造函数</span><span>//function __construct(){</span><span>//</span><span>//  echo "<br> __construct  无参构造方法被调用";</span><span>//}</span><span>//有参构造函数,一般用来类成员属性的初始化,php中一个类只能存在一个构造方法</span><span><span>function</span><span>__construct</span><span>(<span>$name</span>)</span>{</span><span>$this</span>->name = <span>$name</span>;
  <span>echo</span><span>"<br> __construct  有参构造方法被调用"</span>;
}

<span>//实例方法</span><span><span>function</span><span>instanceFun</span><span>()</span>{</span><span>echo</span><span>"<br>实例方法被调用"</span>;
  <span>echo</span><span>"<br>实例方法中访问常量:"</span>.<span>self</span>::PATH;
  <span>echo</span><span>"<br>实例方法中访问实例属性:{$this->name}"</span>;
  <span>echo</span><span>"<br>实例方法中访问静态属性:"</span>.<span>self</span>::<span>$count</span>;<span>//self就代表类本身</span><span>//实例方法中调用静态方法</span><span>self</span>::staticFun();

}

<span>//静态方法</span><span>static</span><span><span>function</span><span>staticFun</span><span>()</span>{</span><span>echo</span><span>"<br>静态方法被调用"</span>;
  <span>echo</span><span>"<br>静态方法中访问常量:"</span>.<span>self</span>::PATH;
  <span>echo</span><span>"<br>静态方法中访问静态属性:"</span>.<span>self</span>::<span>$count</span>;
  <span>//注意:静态方法中无法访问实例属性</span>}

<span>//析构方法,当对象被销毁的时候由系统去调用</span><span>//通常代码执行完,对象就会被销毁</span><span><span>function</span><span>__destruct</span><span>()</span>{</span><span>echo</span><span>"<br>对象 {$this->name} 被销毁"</span>;
}


}


<span>$object</span> = <span>new</span> C(<span>"哈利波特"</span>);
<span>$object</span>->instanceFun();
<span>echo</span><span>"<hr>"</span>;
C::staticFun();

<span>//下面代码会使$object指向的对象被销毁</span><span>//$object = null;</span><span>//$object = new C("秦始皇");;</span><span>//$object = 3;</span><span>//unset($object);</span><span>echo</span><span>"<br>程序最后一行代码。。。"</span>;</span></code>
Copy after login

面向对象,php面向对象教程,php面向对象实例,php面向对象视频,php面向对象编程实例,php面向对象编程,深入php面向对象,php是面向对象的吗,php 面向对象 多

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the object-oriented members of PHP, including object-oriented and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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