Rumah > php教程 > php手册 > teks badan

PHP面向对象中的重要知识点(一)

WBOY
Lepaskan: 2016-06-06 19:36:10
asal
998 orang telah melayarinya

1. __construct: 内置构造函数,在 对象 被创建时自动调用。见如下代码: ? php class ConstructTest { private $arg1 ; private $arg2 ; public function __construct( $arg1 , $arg2 ) { $this -arg1 = $arg1 ; $this -arg2 = $arg2 ; print "__construct

1. __construct: 

     内置构造函数,在对象被创建时自动调用。见如下代码:

<span>php
</span><span>class</span><span> ConstructTest {
    </span><span>private</span> <span>$arg1</span><span>;
    </span><span>private</span> <span>$arg2</span><span>;

    </span><span>public</span> <span>function</span> __construct(<span>$arg1</span>, <span>$arg2</span><span>) {
        </span><span>$this</span>->arg1 = <span>$arg1</span><span>;
        </span><span>$this</span>->arg2 = <span>$arg2</span><span>;
        </span><span>print</span> "__construct is called...\n"<span>;
    }
    </span><span>public</span> <span>function</span><span> printAttributes() {
        </span><span>print</span> '$arg1 = '.<span>$this</span>->arg1.' $arg2 = '.<span>$this</span>->arg2."\n"<span>;
    }
}
</span><span>$testObject</span> = <span>new</span> ConstructTest("arg1","arg2"<span>); 
</span><span>$testObject</span>->printAttributes();
Salin selepas log masuk

     运行结果如下:

Stephens-<span>Air:Desktop$ php Test.php 
__construct is called...
$arg1 </span>= arg1 $arg2 = arg2
Salin selepas log masuk

2. parent: 

     用于在子类中直接调用父类中的方法,功能等同于Java中的super。 

<span>php
</span><span>class</span><span> BaseClass {
    </span><span>protected</span> <span>$arg1</span><span>;
    </span><span>protected</span> <span>$arg2</span><span>;

    </span><span>function</span> __construct(<span>$arg1</span>, <span>$arg2</span><span>) {
        </span><span>$this</span>->arg1 = <span>$arg1</span><span>;
        </span><span>$this</span>->arg2 = <span>$arg2</span><span>;
        </span><span>print</span> "__construct is called...\n"<span>;
    }
    </span><span>function</span><span> getAttributes() {
        </span><span>return</span> '$arg1 = '.<span>$this</span>->arg1.' $arg2 = '.<span>$this</span>-><span>arg2;
    }
}

</span><span>class</span> SubClass <span>extends</span><span> BaseClass {
    </span><span>protected</span> <span>$arg3</span><span>;

    </span><span>function</span> __construct(<span>$baseArg1</span>, <span>$baseArg2</span>, <span>$subArg3</span><span>) {
        parent</span>::__construct(<span>$baseArg1</span>, <span>$baseArg2</span><span>);
        </span><span>$this</span>->arg3 = <span>$subArg3</span><span>;
    }
    </span><span>function</span><span> getAttributes() {
        </span><span>return</span> parent::getAttributes().' $arg3 = '.<span>$this</span>-><span>arg3;
    }
}
</span><span>$testObject</span> = <span>new</span> SubClass("arg1","arg2","arg3"<span>); 
</span><span>print</span> <span>$testObject</span>->getAttributes()."\n";
Salin selepas log masuk

     运行结果如下:

Stephens-<span>Air:Desktop$ php Test.php 
__construct is called...
$arg1 </span>= arg1 $arg2 = arg2 $arg3 = arg3
Salin selepas log masuk

3. self:

     在类内调用该类静态成员和静态方法的前缀修饰,对于非静态成员变量和函数则使用this。 

<span>php
</span><span>class</span><span> StaticExample {
    </span><span>static</span> <span>public</span> <span>$arg1</span> = "Hello, This is static field.\n"<span>;
    </span><span>static</span> <span>public</span> <span>function</span><span> sayHello() {
        </span><span>print</span> self::<span>$arg1</span><span>;
    }
}

</span><span>print</span> StaticExample::<span>$arg1</span><span>;
StaticExample</span>::sayHello();
Salin selepas log masuk

     运行结果如下:

Stephens-<span>Air:Desktop$ php Test.php 
Hello, This is static field.
Hello, This is static field.</span>
Salin selepas log masuk

4. static:

     这里介绍的static关键字主要用于PHP 5.3以上版本新增的延迟静态绑定功能。请看一下代码和关键性注释。

<span>php
</span><span>abstract</span> <span>class</span><span> Base {
    </span><span>public</span> <span>static</span> <span>function</span><span> getInstance() {
        </span><span>//</span><span>这里的new static()实例化的是调用该静态方法的当前类。</span>
        <span>return</span> <span>new</span> <span>static</span><span>();
    }
    </span><span>abstract</span> <span>public</span> <span>function</span><span> printSelf(); 
}

</span><span>class</span> SubA <span>extends</span><span> Base {
    </span><span>public</span> <span>function</span><span> printSelf() {
        </span><span>print</span> "This is SubA::printSelf.\n"<span>;
    }
}

</span><span>class</span> SubB <span>extends</span><span> Base {
    </span><span>public</span> <span>function</span><span> printSelf() {
        </span><span>print</span> "This is SubB::printSelf.\n"<span>;
    }
}

SubA</span>::getInstance()-><span>printSelf();
SubB</span>::getInstance()->printSelf();
Salin selepas log masuk

     运行结果如下:

Stephens-<span>Air:Desktop$ php Test.php 
This is SubA::printSelf.
This is SubB::printSelf.</span>
Salin selepas log masuk

     static关键字不仅仅可以用于实例化。和self和parent一样,static还可以作为静态方法调用的标识符,甚至是从非静态上下文中调用。在该场景下,self仍然表示的是当前方法所在的类。见如下代码: 

<span>php
</span><span>abstract</span> <span>class</span><span> Base {
    </span><span>private</span> <span>$ownedGroup</span><span>;
    </span><span>public</span> <span>function</span><span> __construct() {
        </span><span>//</span><span>这里的static和上面的例子一样,表示当前调用该方法的实际类。
        //需要另外说明的是,这里的getGroup方法即便不是静态方法,也会得到相同的结果。然而倘若
        //getGroup真的只是普通类方法,那么这里还是建议使用$this。</span>
        <span>$this</span>->ownedGroup = <span>static</span>::<span>getGroup();
    }
    </span><span>public</span> <span>function</span><span> printGroup() {
        </span><span>print</span> "My Group is ".<span>$this</span>->ownedGroup."\n"<span>;
    }
    </span><span>public</span> <span>static</span> <span>function</span><span> getInstance() {
        </span><span>return</span> <span>new</span> <span>static</span><span>();
    }
    </span><span>public</span> <span>static</span> <span>function</span><span> getGroup() {
        </span><span>return</span> "default"<span>;
    }
}

</span><span>class</span> SubA <span>extends</span><span> Base {
}

</span><span>class</span> SubB <span>extends</span><span> Base {
    </span><span>public</span> <span>static</span> <span>function</span><span> getGroup() {
        </span><span>return</span> "SubB"<span>;
    }
}

SubA</span>::getInstance()-><span>printGroup();
SubB</span>::getInstance()->printGroup(); 
Salin selepas log masuk

     运行结果如下:

Stephens-<span>Air:Desktop$ php Test.php 
My Group is default
My Group is SubB</span>
Salin selepas log masuk

5. __destruct:

     析构方法的作用和构造方法__construct刚好相反,它只是在对象被垃圾收集器收集之前自动调用,我们可以利用该方法做一些必要的清理工作。

<span>php
</span><span>class</span><span> TestClass {
    </span><span>function</span><span> __destruct() {
        </span><span>print</span> "TestClass destructor is called.\n"<span>;
    }
}

</span><span>$testObj</span> = <span>new</span><span> TestClass();
</span><span>unset</span>(<span>$testObj</span><span>);
</span><span>print</span> "Application will exit.\n";
Salin selepas log masuk

     运行结果如下:

Stephens-<span>Air:Desktop$ php Test.php 
TestClass destructor is called.
Application will exit.</span>
Salin selepas log masuk

6. __clone:

     在PHP 5之后的版本中,对象之间的赋值为引用赋值,即赋值后的两个对象将指向同一地址空间,如果想基于对象赋值,可以使用PHP提供的clone方法。该方法将当前对象浅拷贝之后的副本返回,如果想在clone的过程中完成一些特殊的操作,如深拷贝,则需要在当前类的声明中实现__clone方法,该方法在执行clone的过程中会被隐式调用。另外需要格外注意的是,__clone方法是作用再被拷贝的对象上,即赋值后的对象上执行。

<span>php
</span><span>class</span><span> InnerClass {
    </span><span>public</span> <span>$id</span> = 10<span>;
    </span><span>public</span> <span>function</span><span> printSelf() {
        </span><span>print</span> '$id = '.<span>$this</span>->id."\n"<span>;
    }
}

</span><span>class</span><span> OuterClass {
    </span><span>public</span> <span>$innerClass</span><span>;
    </span><span>public</span> <span>function</span><span> __construct() {
        </span><span>$this</span>->innerClass = <span>new</span><span> InnerClass();
    }
    </span><span>public</span> <span>function</span><span> __clone() {
        </span><span>$this</span>->innerClass = <span>clone</span> <span>$this</span>-><span>innerClass;
        </span><span>print</span> "__clone is called.\n"<span>;
    }
}

</span><span>$outerA</span> = <span>new</span><span> OuterClass();
</span><span>print</span> "Before calling to clone.\n"<span>;
</span><span>$outerB</span> = <span>clone</span> <span>$outerA</span><span>;
</span><span>print</span> "After calling to clone.\n"<span>;
</span><span>$outerA</span>->innerClass->id = 20<span>;
</span><span>print</span> "In outerA: "<span>;
</span><span>$outerA</span>->innerClass-><span>printSelf();
</span><span>print</span> "In outerB: "<span>;
</span><span>$outerB</span>->innerClass->printSelf();
Salin selepas log masuk

     运行结果如下:

Stephens-<span>Air:Desktop$ php Test.php 
Before calling to clone.
__clone is called.
After calling to clone.
In outerA: $</span><span>id</span> = <span>20</span><span>
In outerB: $</span><span>id</span> = <span>10</span>
Salin selepas log masuk

7. const:

    PHP5可以在类中定义常量属性。和全局常量一样,一旦定义就不能改变。常量属性不需要像普通属性那样以$开头,按照惯例,只能用大写字母来命名常量。另外和静态属性一样,只能通过类而不能通过类的实例访问常量属性,引用常量时同样也不需要以$符号作为前导符。另外常量只能被赋值为基础类型,如整型,而不能指向任何对象类型。

<span>php
</span><span>class</span><span> TestClass {
    </span><span>const</span> AVAILABLE = 0<span>;
}

</span><span>print</span> "TestClass::AVAILABLE = ".TestClass::AVAILABLE."\n";
Salin selepas log masuk

    运行结果如下:

0Stephens-<span>Air:Desktop$ php Test.php 
TestClass::AVAILABLE </span>= <span>0</span>
Salin selepas log masuk

注:该Blog中记录的知识点,是在我学习PHP的过程中,遇到的一些PHP和其他面向对象语言相比比较特殊的地方,或者是对我本人而言确实需要簿记下来以备后查的知识点。虽然谈不上什么深度,但还是希望能与大家分享。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan