<?php class me { static public $me="surfchen"; static protected $wife="wolfzeus"; public function MySkill() { return "programme"; } protected function MyWifeSkill() { return "basketball"; } private function OurSleep() { return "Love"; } } class myson extends me { function __construct() { $this->father=me::$me; } protected function mother() { return $this->mother=me::$wife; } public function SonSkill() { return me::MySkill()." and ".me::MyWifeSkill(); } } $me=new me; $son=new myson; echo "My name is ".me::$me.",good at ".$me->MySkill()."<br />"; echo "I have a wife,but I can't tell you what her name is and what skill she has.(MyWifeSkill() is protected)<br />"; echo "Our loves and lives is private,so......<br />"; echo "We have a son,he good at ".$son->SonSkill()."."; ?>
谢谢surfchen(冲浪) 和上面的几位,我明白了。但是我在测试的时候,出现了以下错误: Parse error: parse error, unexpected T_STATIC, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in d:\usr\www\html\test\test.php on line 4我用的源码是: <?php class test { static public $one="surfchen"; static private $tow="wolfzeus"; function __construct() { $this->user=self::$tow; } } echo test::$one;//output surfchen $test=new test; echo $test->user;//output wolfzeus ?> 是不是我的PHP用问题呢?
拿本C++或者JAVA的基础书看看就知道了
我的代码都是以PHP5为平台的...PHP4不兼容...不过从代码上说跟PHP4也差不多....
vb里面没有类似于::的东西 php4的例子class test{ function func1(){ echo "this is func1"; } var $field=10; } echo test::field; test::func1();
::在PHP4里的一个作用是继承被覆写的父类的方法<?php class father { function skill() { return "php"; } }class son extends father { function skill() { return "wml"; } function FatherSkill() { return parent::skill(); } } $son=new son; echo $son->FatherSkill(); ?>
thing.text="hi!"
PHP
thing->text="hi!"
->是调用类的属性和方法吧 $clss1->a; $class1->do();
如:
class mycls{
var $a=0;
function exc($str){
echo $str;
}
}
$cls=new mycls;
用exc和a的时候都是 $cls->exc() or $cls->a;
但什么时候用::呢?能举个小例子吗?
class test
{
static public $one="surfchen";
static private $tow="wolfzeus";
function __construct()
{
$this->user=self::$tow;
}
}
echo test::$one;//output surfchen
$test=new test;
echo $test->user;//output wolfzeus
?>
class me
{
static public $me="surfchen";
static protected $wife="wolfzeus";
public function MySkill()
{
return "programme";
}
protected function MyWifeSkill()
{
return "basketball";
}
private function OurSleep()
{
return "Love";
}
}
class myson extends me
{
function __construct()
{
$this->father=me::$me;
}
protected function mother()
{
return $this->mother=me::$wife;
}
public function SonSkill()
{
return me::MySkill()." and ".me::MyWifeSkill();
}
}
$me=new me;
$son=new myson;
echo "My name is ".me::$me.",good at ".$me->MySkill()."<br />";
echo "I have a wife,but I can't tell you what her name is and what skill she has.(MyWifeSkill() is protected)<br />";
echo "Our loves and lives is private,so......<br />";
echo "We have a son,he good at ".$son->SonSkill().".";
?>
Parse error: parse error, unexpected T_STATIC, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in d:\usr\www\html\test\test.php on line 4我用的源码是:
<?php
class test
{
static public $one="surfchen";
static private $tow="wolfzeus";
function __construct()
{
$this->user=self::$tow;
}
}
echo test::$one;//output surfchen
$test=new test;
echo $test->user;//output wolfzeus
?>
是不是我的PHP用问题呢?
php4的例子class test{
function func1(){
echo "this is func1";
}
var $field=10;
}
echo test::field;
test::func1();
class father
{
function skill()
{
return "php";
}
}class son extends father
{
function skill()
{
return "wml";
}
function FatherSkill()
{
return parent::skill();
}
}
$son=new son;
echo $son->FatherSkill();
?>