PHP中的类,函数,静态属性,静态函数,变量赋值
2015-11-21 11:03
639 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ") ";
} else {
echo "$this is not defined. ";
}
}
}
class B
{
function bar()
{
A::foo();
}
}
$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>
<hr />
<?php
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
<hr />
<?php
class SimpleClass
{
/*
//错误赋值
$var=$myvar 属性的值不能是变量!!!这是最基础的知识。
注意:成员属性不可以是带运算符的表达式、变量、方法或函数的调用。 如:
public $var3 = 1+2;
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
正确定义方式:
public $var6 = 100; //普通数值(4个标量:整数、浮点数、布尔、字串)
public $var6 = myConstant; //常量
public $var7 = self::classConstant; //静态属性
public $var8 = array(true, false); //数组
*/
// 成员声明
public static $var = 'a default value';
// 方法声明
public static function displayVar() {
echo $this->var;
}
public function prit()
{
$var6 = self::$var;
echo self::$var;
}
// 正确的类成员定义:
public $var6 ;
public $var7 = 'ggggg';
public $var8 = array(true, false);
}
class ExtendClass extends SimpleClass
{
// Redefine the parent method
static function displayVar()
{
echo "Extending class ";
parent::displayVar();
}
}
$extended = new ExtendClass();
$extended->displayVar();
?>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ") ";
} else {
echo "$this is not defined. ";
}
}
}
class B
{
function bar()
{
A::foo();
}
}
$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>
<hr />
<?php
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
<hr />
<?php
class SimpleClass
{
/*
//错误赋值
$var=$myvar 属性的值不能是变量!!!这是最基础的知识。
注意:成员属性不可以是带运算符的表达式、变量、方法或函数的调用。 如:
public $var3 = 1+2;
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
正确定义方式:
public $var6 = 100; //普通数值(4个标量:整数、浮点数、布尔、字串)
public $var6 = myConstant; //常量
public $var7 = self::classConstant; //静态属性
public $var8 = array(true, false); //数组
*/
// 成员声明
public static $var = 'a default value';
// 方法声明
public static function displayVar() {
echo $this->var;
}
public function prit()
{
$var6 = self::$var;
echo self::$var;
}
// 正确的类成员定义:
public $var6 ;
public $var7 = 'ggggg';
public $var8 = array(true, false);
}
class ExtendClass extends SimpleClass
{
// Redefine the parent method
static function displayVar()
{
echo "Extending class ";
parent::displayVar();
}
}
$extended = new ExtendClass();
$extended->displayVar();
?>
</body>
</html>
相关文章推荐
- [转]phpcms 判断用户登录代码
- php中curl的详细解说
- php中curl的详细解说
- thinkphp自动跳到首页 --开启域名部署/子域名部署/泛域名部署/IP访问部署
- wamp打开phpmyadmin空白
- php发邮件
- php eval使用
- PHP中的逻辑判断函数empty() isset() is_null() ==NULL ===NULL
- thinkphp3.2 home为默认模块,怎么隐藏掉home,在url里面。
- php导出execl
- php中的递归
- Ubuntu 14.04 FTP服务器--vsftpd的安装和配置
- Thinkphp中去除URL里的index.php
- ContentProvider 基础
- php后台“爬虫”模拟登录第三方系统(三)---模拟登录学校正方系统
- php常用函数
- 阿里云自定义日记文件无法通过ftp下载
- 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明
- 阿里云本地FTP怎么连接?通用win7,win8,win8.1,win10
- PHP 判断常量,变量和函数是否存在