Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP สามารถกำหนดค่าคงที่ (Constant) ใน Constructor ของคลาสได้หรือไม่

ไม่สามารถกำหนดค่าคงที่ใน Constructor ของคลาสได้ ควรกำหนดค่าคงที่ที่ระดับคลาส (class level)

สมมติว่ามีคลาสดังนี้

class ConstantDemo {}

กำหนดค่าคงที่ในคลาส

class ConstantDemo { const LANGUAGE_NAME = "PHP"; }

ตัวอย่างโค้ด

<!DOCTYPE html> <html> <body> <?php class ConstantDemo { const LANGUAGE_NAME = "PHP"; } echo "The language Name is=", ConstantDemo::LANGUAGE_NAME; ?> </body> </html>

ผลลัพธ์

The language Name is=PHP