首页 > 编程 > PHP > 正文

PHP的基本常识小结

2020-03-22 16:59:47
字体:
来源:转载
供稿:网友
这些PHP的概念,有些刚开始比较难懂,很难理解,我把他们都列出来,希望能帮助一些人,在前进的路上少点荆棘。1. variable variables(变量的变量)variable_variables.php更多数组函数参考3. dates and times (时间和日期)

有3种方法可以创建一个unix time(从1970/1/1 到现在的秒数)

time(); 返回当前的时间戳

mktime($hr, $min, $sec, $month, $day, $year); mktime(6,30,0,5,22,2012) 返回2012 5/22 6:30:00 的时间戳

strtotime($string); strtotime("+1 day") 返回明天这个时候的时间戳 更多 'last Monday' 'lasy Year'

checkdate($month, $day, $year); 验证一个日期是否为真 checkdate(5,32,2012) ? 'true' : 'false'; // return false

得到了时间戳后,我们需要对它进行转化为可读的,如2012/5/22

我们有2种方法 date($format, $timestamp) ; strftime($format [,$timestamp])

推荐用第2种,strftime("%Y-%m-%d %H:%M:%S"); // return 2012-05-22 15:46:40

更多时间日期参考5. server variables (服务器和执行环境信息)

$_SERVER

server_variables.php

更多详细信息6.variable_scope(变量的作用域 global html' target='_blank'>static)static_variables.php
更多详细信息

7.reference(引用)

variable_reference.php
更多详细信息

对象 OOP1.Fatal error: Using $this when not in object context这个错误刚学 OOP 肯定容易出现,因为有个概念你没有真正理解。 类的可访问性(accessible),也可以说是作用域, 你还可以认为是 1个 中国人 在国外,他不属于哪个文化,他不讲外语(可能他知道点);但是他无法通过自己跟老外沟通,因为他们不是在一个共同国度出生。
那么错误是如何发生的呢?看下面的例子:复制代码 代码如下:
?php
class Trones{
static public $fire = "I am fire.";
public $water = "I am water";

static function getFire( ) {
return $this- fire ; // wrong
}
static function getWater( ) {
return $self::water ; // wrong
}

static function Fire( ) {
return self::$fire ; // be sure you use self to access the static property before you invoke the function
}
}

/*
Fatal error: Using $this when not in object context
*/
//echo Trones::getFire( ) ;
//echo Trones::getWater( ) ;

// correct
echo Trones::Fire( );
echo " br /
$trones = new Trones ;
$trones- fire ; // Notice: Undefined property: Trones::$fire (base on defferent error setting) simple is error
echo Trones::$fire ;
这个错误很经典, 也很实用,先看 static 的定义:Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).翻译:定义一个类的属性或方法为静态时,可以使他们在不需要初始化一个类时就能直接访问 。一个被定义为了静态的属性不能被类的对象用对象操作符访问* - *,(可以通过静态的方法访问)。例子说明:
7行 10行 犯了同一个错误,第一个是用对象操作符来访问静态变量。你看看定义,$this 是一个伪变量 相当于 object,一个实例。你用对象操作符 - 访问就会报错。同样你也不能用 静态操作符 :: 来访问一个公共变量 。 正确的访问应该是 14行 25行,一个是在类的定义里访问(self:: === Trones::),一个是在类的外部访问。对于继承类,以上的规则同样适合。2.Fatal error: Call to private method
最近有部连续剧很好看,叫权利的游戏,我们假设有 3方人马, 7个国王, 平民, 龙女。 他们三方人马在下面争夺最终的胜利, 也就是王冠。下面的故事还有一个标题:类的可见性(visibility) 你如果知道最终的答案,解释部分你可以略过了。复制代码 代码如下:
?php
class Trones {
protected $fire = " fire ";
public $water = " water " ;
static private $trones = "Trones";

protected function getFire( ) {
$this- fire ;
}

static public function TheDragenOfMather( ) {
return __METHOD__." use ".$this- getFire()." gets the ".self::getTrones( ) ;
}

static public function getWater( ) {
return __METHOD__ ;
}

static private function getTrones( ) {
return self::$trones ;
}

}

class Kings extends Trones {
static function TheSevenKing( ) {
return __METHOD__."gets the ".self::getTrones( );
}
}

class People extends Trones{
static function ThePeople( ) {
return __METHOD__."gets the ".self::getTrones( );
}
}
echo Kings::TheSevenKing( ) ;
echo Trones::TheDragenOfMather( ) ;
echo People::ThePeople( ) ;


正确答案是:7国征战 内斗,平民死伤无数,龙女想乘机渔翁得利;可惜 最终谁也没有得到皇冠和胜利。哈哈。当static 碰到 private ,结合产生复杂,也产生美;就像抽象的人,像我们大学老师讲的数学课;(不过网易的公开数学课很好)如果想要龙女 获得最后的胜利, 你只要帮她一把 将13行的 $this- getFire() 这部分去掉就可以了。同样的道理 你无法在一个静态函数里 使用任何对象操作符。怎么使人民获得王冠呢? 你去奋斗吧!如果你不构建大型的框架和网站 这些概念比如 Interface Implement abstract 。。。 你还是不知道的好。PHP教程

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表