您的位置:首页 > 编程语言 > PHP开发

PHP作用域限定符::的几个程序例子

2013-11-02 13:29 375 查看
双冒号::被认为是作用域限定操作符,用来指定类中不同的作用域级别。::左边表示的是作用域,右边表示的是访问的成员。

系统定义了两个作用域,self和parent。self表示当前类的作用域,在类之外的代码是不能使用这个操作符的。

Program List:使用self作用域访问父类中的函数

01
<?php
02
    
class

NowaClass
03
    
{
04
        
function

nowaMethod()
05
        
{
06
            
print
'我在类 NowaClass 中声明了。'
;
07
        
}
08
    
}
09
    
 
10
    
class

ExtendNowaClass
extends
NowaClass
11
    
{
12
        
function

extendNowaMethod()
13
        
{
14
            
print
'extendNowaMethod 这个方法在 ExtendNowaClass这个类中声明了。'
;
15
            
self::nowaMethod();
16
        
}
17
    
}
18
    
 
19
    
ExtendNowaClass::extendNowaMethod();
20
    
 
21
?>
程序运行结果:

1
extendNowaMethod 这个方法在 ExtendNowaClass这个类中声明了。
2
我在类 NowaClass 中声明了。
parent这个作用域很简单,就是派生类用来调用基类的成员时候使用。

Program List:使用parent作用域

01
<?php
02
    
class

NowaClass
03
    
{
04
        
function

nowaMethod()
05
        
{
06
            
print
'我是基类的函数。'
;
07
        
}
08
    
}
09
    
 
10
    
class

ExtendNowaClass
extends
NowaClass
11
    
{

12
        
function

extendNowaMethod()
13
        
{
14
            
print
'我是派生类的函数。'
;
15
            
parent::nowaMethod();
16
        
}
17
    
}
18
    
 
19
    
$obj

=
new
ExtendNowaClass();
20
    
$obj

-> extendNowaMethod();
21
    
 
22
?>
程序运行结果:

1
我是派生类的函数。
2
我是基类的函数。

Program List:用基类的方法访问派生类的静态成员

如何继承一个存储位置上的静态属性。

01
<?php
02
class
Fruit

03
{
04
    
public

function
get()
05
    
{
06
        
echo

$this
->connect();
07
    
}
08
}
09
class
Banana
extends
Fruit
10
{
11
    
private

static
$bananaColor
;
12
    
public

function
connect()
13
    
{
14
        
return

self::
$bananaColor

=
'yellow'
;
15
    
}
16
}
17
class
Orange
extends
Fruit{
18
    
private

static
$orange_color
;
19
    
public

function
connect()
20
    
{
21
        
return

self::
$orange_color

=
'orange'
;
22
    
}
23
}
24
$banana
=

new
Banana();
25
$orange
=

new
Orange();
26
$banana
->get();
27
$orange
->get();
28
?>
程序运行结果:

1
yellow
2
orange。

Program List:静态函数初始化

在一个类中初始化静态变量比较复杂,你可以通过创建一个静态函数创建一个静态的构造器,然后在类声明后马上调用它来实现初始化。

01
<?php
02
class
Fruit

03
{
04
    
private

static
$color

=
"White"
;
05
    
private

static
$weigth
;
06
    
public

static
function
init()
07
    
{
08
        
echo

self::
$weigth

=self::
$color
.

" Kilogram!"
;
09
    
}
10
}
11
Fruit::init();
12
?>
程序运行结果:

1
White Kilogram!

Program List:一个简单的单例模式例子

这个应该可以帮到某些人吧。

01
<?php
02
class
Fruit
03
{
04
    
private

static
$instance

=null;
05
    
private

function
__construct()
06
    
{
07
      
$this
-> color =
'Green'
;
08
    
}
09
    
public

static
function
getInstance()
10
    

c799
{
11
        
if
(self::
$instance

==null)
12
        
{
13
            
print
"Fruitobject created!<br />"
;
14
            
self::
$instance

=
new
self;
15
        
}
16
        
return

self::
$instance
;
17
    
}
18
    
public

function
showColor()
19
    
{
20
        
print
"My color is {$this-> color}!<br>"
;
21
    
}
22
    
public

function
setColor(
$color
)
23
    
{
24
        
$this
-> color =
$color
;
25
    
}
26
}
27
  
$apple

=Fruit::getInstance(); 
// Fruitobject created!
28
  
$apple
-> showColor();
// My color is Green!
29
  
$apple
-> setColor(
"Red"
);
30
  
$apple
-> showColor();
// My color is Red!
31
  
$banana

=Fruit::getInstance();
32
  
$banana
-> showColor();
// My color is Red!
33
  
$banana
-> setColor(
"Yellow"
);
34
  
 
35
  
$apple
-> showColor();
// My color is Yellow!
36
?>
程序运行结果:

1
Fruitobject created!
2
My color is Green!
3
My color is Red!
4
My color is Red!
5
My color is Yellow!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: