<?php
classMyClass { // 定义MyClass类 public$name; // 定义了一个公有属性 protected$_age; // 定义了一个保护属性 private$_email; // 定义了一个私有属性 publicfunctionmy_method() { // 定义my_method方法 } protectedfunction_my_method2() { // 定义_my_method2方法 }}$obj= newMyClass; // 实例化$obj->name = ‘noname‘; // 设置name属性$obj->_age = 12; // 这是错误的,不能设置保护属性$obj->address = ‘King Street.‘// 设置一个未声明的属性是允许的,效果和公有属性类似,但未赋值前不能使用。$obj->my_method() // 调用公有方法$obj->_my_method2(); // 这是错误的,不能调用保护方法Method calls in------------------------------------------interfaces and static classes can be directly:
Class Person
{
public static function Sayname ()
{
echo "hello!";
}
}
Class Emplyee extends person{
}
Person::sayname ();
----------------------------------------------
Interface declaration and invocation in PHP
<?phpinterface a{ //Declaration A Interface //Declare a constant in the interface const B = ' The value of the constant in the interface is: Interface CONSTANT value ';} echo a::b; Output interface constant?>
PHP Object-oriented applications