示例运行截图
1、属性、方法重载,示例代码
class Demo1 {
//------属性重载------------------------------------
private $data = [];
public function __get($name)
{
if(isset($this->data[$name]))
return $this->data[$name];
else
return "您调用的属性不存在...";
}
public function __set($name, $value)
{
$this->data[$name] = $value;
}
//------方法重载------------------------------------
public function __call($name, $args)
{
if(method_exists($this, $name))
$this->$name($args);
else
echo "您调用的方法不存在...", "<br>";
}
public static function __callStatic($name, $args)
{
if(function_exists($name))
self::$name($args);
else
echo "您调用的方法不存在...", "<br>";
}
private function query($args) {
echo "这里是,query()查询方法...", "<br>";
}
private function save($args) {
echo "这里是,save()存储方法...", "<br>";
}
private static function link($args) {
echo "这里是静态,self::link()连接方法...", "<br>";
}
}
echo "<h4>------属性重载,示例演示------------------------------------</h4>";
$obj = new Demo1();
$obj->name = "刘德华";
$obj->age = "60";
$obj->job = "演员";
echo "姓名:". $obj->name.", 年龄:". $obj->age. ", 职业:". $obj->job, "<br>";
echo $obj->gender, "<br>";
echo "<h4>------方法重载,示例演示------------------------------------</h4>";
$obj->query();
$obj->save();
$obj->save123();
Demo1::link("127.0.0.1", "mySql", "root", "root");
示例运行截图
2、命名空间,示例代码
namespace {
class Model {}
echo Model::class, '<br>';
}
namespace www {
class Model {}
echo Model::class, '<br>';
}
namespace www\php {
class Model {}
echo Model::class, '<br>';
}
namespace www\php\cn {
class Model {}
echo Model::class, '<br>';
}