在php的注释中,有一个?>结尾符的问题,就是单行注释的时候,遇到这个结尾符,php就跳出了,注释会被override掉
这里有个评论,讲了这个这样的代码写法:
a trick I have used in all languages to temporarily block out large sections (usually for test/debug/new-feature purposes), is to set (or define) a var at the top, and use that to conditionally comment the blocks; an added benefit over if(0) (samuli’s comment from nov’05) is that u can have several versions or tests running at once, and u dont require cleanup later if u want to keep the blocks in: just reset the var.
personally, I use this more to conditionally include code for new feature testing, than to block it out,,,, but hey, to each their own
this is also the only safe way I know of to easily nest comments in any language, and great for multi-file use, if the conditional variables are placed in an include
for example, placed at top of file:
<?php $ver3 = TRUE;
$debug2 = FALSE;
?>
and then deeper inside the file:
<?php if ($ver3) {
print(“This code is included since we are testing version 3″);
}
?>
所以我们不要注释掉程序语句,而是采用这样的判断来做
http://docs.php.net/manual/zh/language.variables.php 这里有个字符类型转换库的代码
http://docs.php.net/manual/zh/userlandnaming.php mb_convert_encoding(‘&#’.intval($u).’;', ’UTF-8′, ’HTML-ENTITIES’);看不懂的
引用赋值,这个要记录下:
<?php
$foo = ’Bob’; // 将 ’Bob’ 赋给 $foo
$bar = &$foo; // 通过 $bar 引用 $foo
$bar = ”My name is $bar”; // 修改 $bar 变量
echo $bar;
echo $foo; // $foo 的值也被修改
?>
PHP 中最具争议的变化就是从 PHP » 4.2.0 版开始配置文件中 register_globals 的默认值从 on 改为 off 了
<?php
if (isset($_COOKIE['MAGIC_COOKIE'])) {
// MAGIC_COOKIE 来自 cookie
// 这样做是确保是来自 cookie 的数据
} elseif (isset($_GET['MAGIC_COOKIE']) || isset($_POST['MAGIC_COOKIE'])) {
mail(“admin@example.com”, ”Possible breakin attempt”, $_SERVER['REMOTE_ADDR']);
echo ”Security violation, admin has been alerted.”;
exit;
} else {
// 这一次请求中并没有设置 MAGIC_COOKIE 变量
}
?>
当然,单纯地关闭 register_globals 并不代表所有的代码都安全了。对于每一段提交上来的数据,都要对其进行具体的检查。永远要验证用户数据和对变量进行初始化!把error_reporting() 设为 E_NOTICE 级别可以检查未初始化的变量。
http://docs.php.net/manual/zh/language.constants.predefined.php 预定义常量
http://docs.php.net/manual/zh/reserved.variables.php 预定义变量
http://docs.php.net/manual/zh/reserved.variables.server.php server变量,这个又很多需要使用到的哦,server里面的
<?php
// first use encodeURIComponent on javascript to encode the string
// receive json string and prepare it to json_decode
$jsonStr = stripslashes ($_POST['action']);
// decode to php object
$json = json_decode ($jsonStr);
// $json is now a php object
?>
file上传
http://docs.php.net/manual/zh/function.move-uploaded-file.php 这里讨论了file上传时的一些问题和功能完善
http://docs.php.net/manual/zh/features.file-upload.errors.php 各种上传error值
<input type=”file” multiple=”true” name=”files[]“/> 这个在html5中可以支持
数组:
http://docs.php.net/manual/zh/ref.array.php 数组方法
类:
http://docs.php.net/manual/zh/ref.classobj.php 这里有一些类所具有的函数
http://docs.php.net/manual/zh/language.oop5.decon.php 类的构造函数和多构造函数
http://docs.php.net/manual/zh/language.oop5.abstract.php 这里有一段介绍接口和抽象类的
function的内部函数:
未定义的属性赋值时引发:
void __set ( string $name , mixed $value )
mixed __get ( string $name )
使用isset和unset时引发
bool __isset ( string $name )
void __unset ( string $name )
未定义的方法调用时引发:
mixed __call ( string $name , array $arguments )
mixed __callStatic ( string $name , array $arguments )
可以通过foreach来迭代对象中的 public属性
http://www.php.net/~helly/php/ext/spl/ 标准php库。。这里面好多东西。。
要重做数组的迭代器:
首先是iterator接口的实现:
里面有5个方法要实现,他们的首次顺序是:
1 rewind():Rewind the Iterator to the first element.
2 valid():Check if there is a current element after calls to rewind() or next().
3 current():Return the current element.
4 key():Return the key of the current element.
5 next():Move forward to next element.
6 current()
7 valid()
之后的顺序就是从 3 current开始 执行5步,碰到最后一个元素时,next以及next之后都为空
里面有个iteratoraggregate ,看了一下
iteratorAggregate::getIterator()
- Returns:
- an Iterator for the implementing object.
Implemented in ArrayObject.
要使用这个数组iterator 可以实例化这个实现了iterator的类,也可以在写另外一个类(类B),这个类实现iteratorAggregate接口
并在类B中实现接口中的getIterator()方法,这个方法要求返回一个iterator
这样,我们就重写了类中的变量存放数组的遍历方式。在使用foreach的时候,就不一样了。
http://docs.php.net/manual/zh/ref.spl.php 这里有SPL方法,比如class_implements($class_name或者new class) 和class_parents($class_name或者new class)
http://docs.php.net/manual/zh/language.oop5.patterns.php 这里讲模式,讨论了很多单例模式。。不太懂
魔术方法:
__construct, __destruct (参看 构造方法和析构方法), __call, __callStatic, __get, __set, __isset, __unset (参看 重载), __sleep, __wakeup, __toString,__set_state 和 __clone
http://docs.php.net/manual/zh/language.oop5.magic.php
http://docs.php.net/manual/zh/language.oop5.typehinting.php#86216 typehint,有段代码Daniel写的,不过看不懂。。
他的代码意图:
People often ask about scalar/basic typehints. Here is a drop in class that I use in my MVC framework that will enable typehints through the use of a custom error handler.
1) Because people are sick of using the is_* functions to validate parameters.
2) Reduction of redundant coding for defensive coders.
3) Functions/Methods are self defining/documenting as to required input.
意图就是:摒弃is_*function ,用typehint来确定参数的基础类
静态调用:
http://docs.php.net/manual/zh/language.oop5.late-static-bindings.php
有3个例子,第一个例子告诉我了,这个有些类似$this来调用,不过是用来调用static方法的。
第二个例子不好,我把static换成self,结果是一样的。
第三个例子告诉我,用self和parent,结果是一样的
最后结果就是,static的静态方法,有越级的功能,子类的static方法可以覆盖父类同名static方法