The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden members or methods of a class.
<?phpวิธีใช้ ชื่อClass::ชื่อMethod();
class A{
function show(){
echo "Hi Narisa";
}
} //End Class A
echo A::show(); //<--- เรียกใช้นะครับ
?>
====================================
Static Keyword
Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).
For compatibility with PHP 4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public.
Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static.
Static properties cannot be accessed through the object using the arrow operator ->.
Calling non-static methods statically generates an E_STRICT level warning.
As of PHP 5.2.4, it's possible to reference the class using a variable.
Example 19.15. Static member exampleแปลคราวๆ น่าจะประมานว่า :: ใช้สำหรับเรียก static method ที่ไม่จำเป็นต้องสร้าง object ขึ้นมาก่อน
<?phpclass Foo { public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } class Bar extends Foo { public function fooStatic() { return parent::$my_static; } } print Foo::$my_static . "\n"; $foo = new Foo(); print $foo->staticValue() . "\n"; print $foo->my_static . "\n"; // Undefined "Property" my_static // $foo::my_static is not possible print Bar::$my_static . "\n"; $bar = new Bar(); print $bar->fooStatic() . "\n"; $classname = "Bar"; print $classname::$my_static; ?>
operator -> ใช้สำหรับ access ข้อมูลของ object
ไม่มีความคิดเห็น:
แสดงความคิดเห็น