1. PHP中值的初始化
ini_set()函数用于改变php.ini中属性PHP_INI_USER配置选项,这样的设置将仅仅影响被设置的脚本。一旦脚本执行完毕,该变量将自动恢复到原始值。
defined 的使用 defined('DS') || define('DS', DIRECTORY_SEPARATOR); 如果系统设置了DS则不再设置,否则执行后面的语句.
2. <s>PHP中文件的加载require_once,require, include, include_once。</s>
require 和included的相同点:
- 文件的加载是php语言的特殊结构,参数不需要加括号, 即require 'init.php';
- 当一个文件被包含时,其中所包含的代码继承了 include 所在行的变量范围。从该处开始,调用文件在该行处可用的任何变量在被调用的文件中也都可用。不过所有在包含文件中定义的函数和类都具有全局作用域,在处理require和include时,程序换由php模式转换成html模式,因此在被包含的文件中已经要加上<?php ?>
- 文件默认先在include_path下找,若未找到则在当前目录下找。
require 和 include的区别:
1. require包含的文件时在函数编译器加载进来的,而include是在程序执行器。因此可根据include的特点选择性的加载文件进来;
2. 在错误处理上,require没有返回值,而include有,require会直接中止程序,而include 则是发出一个警告,继续执行。对于include的返回值,看如下示例代码:
return.php
<?php
$var = 'PHP';
return $var;
?>
noreturn.php
<?php
$var = 'PHP';
?>
testreturns.php
<?php
$foo = include 'return.php';
echo $foo; // prints 'PHP'
$bar = include 'noreturn.php';
echo $bar; // prints 1
?>
要在脚本中自动包含文件,参见 php.ini 中的 auto_prepend_file 和 auto_append_file 配置选项。
include 和 include_once的区别(require和require_once也是一样的)
唯一区别是如果该文件中已经被包含过,则不会再次包含。
3. 类文件的自动加载function __autoload(string $classname)
当你使用一个未被定义的类时,将默认执行这个函数,在这个函数中,你可以自己定义如何去包含那些类文件,这个函数
使用于去包含多个类文件的时候,示例如下:
./myClass.php
<?php
class myClass {
public function __construct() {
echo "myClass init'ed successfuly!!!";
}
}
?>
./index.php
<?php
// we've writen this code where we need
function __autoload($classname) {
$filename = "./". $classname .".php";
include_once($filename);
}
// we've called a class ***
$obj = new myClass();
?>
同样,如果你想在类中单独实现自动加载函数,可用spl_autoload_register ([ callback $autoload_function
] ) 去注册一个自动加载函数,而无需去实现__autoload,因为__autoload无法在类中使用,因此在类中要实现自动加载函数时可以用此注册函数,若参数为空,则默认调用__autoload.
4. this, self, parent的区别
这三个名称都是php涉及到oop编程时用到的。
$this 和C++中的一致,指向被实例化的自身,用this可以访问本类中的private参数,函数等。
self则指向未被实例化的类本身,一般和static配合起来使用,用来实现设计模式中的单例模式不错。