PHP类的自动加载
通常我们写一个类如下:
a.php
class A
{
public function __construct()
{
echo "hello world!";
}
}
page.php
require("a.php");
$a = new A();
我们是通过手工引用某个类的文件来实现函数或者类的加载
但是当系统比较庞大,以及这些类的文件很多的时候,这种方式就显得非常不方便了
于是PHP5提供了一个::auotload::的方法
我们可通过编写该方法来自动加载当前文件中使用的类文件
page.php
function __autoload($classname)
{
$class_file = strtolower($classname).".php";
if (file_exists($class_file)){
require_once($class_file);
}
}
$a = new A();
这样,当使用类A的时候,发现当前文件中没有定义A,则会执行autoload函数,并根据该函数实现的方式,去加载包含A类的文件
同时,我们可以不使用该方法,而是使用我们自定义的方法来加载文件,这里就需要使用到函数
bool spl_autoload_register ( [callback $autoload_function] )
page.php
function my_own_loader($classname)
{
$class_file = strtolower($classname).".php";
if (file_exists($class_file)){
require_once($class_file);
}
}
spl_autoload_register("my_own_loader");
$a = new A();
实现的是同样的功能
自定义的加载函数还可以是类的方法
class Loader
{
public static function my_own_loader($classname)
{
$class_file = strtolower($classname).".php";
if (file_exists($class_file)){
require_once($class_file);
}
}
}
// 通过数组的形式传递类和方法的名称
spl_autoload_register(array("my_own_loader","Loader"));
$a = new A();
相关文档:
看了些PHP的基础知识,自己在这里总结下:
1,在HTML嵌入PHP脚本有三种办法:
<script language="php">
//嵌入方式一
echo("test");
</script>
<?
//嵌入方式二
echo "<br>test2";
?>
<?php
//嵌入方式三
echo "<br>test3";
?>
还有一种嵌入方式,即使用 ......
$thunder = ("Thunder://QUFodHRwOi8vNjAuMTkxLjYwLjEwODo4MDgwL3hweGlhemFpL0RlZXBpbl9HaG9zdF9YUF9WMTguMC5pc29aWg==");
//解密它
$thunder = trim($thunder,'Thunder://');
$c_thunder = base64_decode($thunder);
$c_thunder = ltrim(rtrim($c_thunder,'ZZ'),'AA');
//out [url]http://60.191.60.108:8080/xpxi ......
花了几个小时的时间研究一下,发现还是比较好用的!
smarty可以很好地将逻辑与表现分离,后台程序员和web前端工程师各干各的事,不像以前,php代码和html代码杂合在一起,后台程序员和前端前序员一起发飙,因为他们的技能要求更高了,都必须要懂对方的语言,呵呵!
网上介绍的配置太哆嗦了,也许是老版本的缘故的,因为我 ......
PHP编程中经常需要用到一些服务器的一些资料,特把$_SERVER的详细参数整理下,方便以后使用。
$_server 代码
1. $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关。
2. $_SERVER['argv'] # 传递给该脚本的参数。
3. $_SERVER['argc'] # 包含传递给程序的命令行参数的个数(如果运行 ......
每个PHP程序员都知道PHP有强大的正则表达式功能,为了以后的工作方便,我从网上整理了关于正则表达式的资料,方便以后工作时的进行资料查阅。
正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。 ......