PHP 经验、技巧整理(一)

       近期工作比较忙,有好几个项目要进行功能调整,另外我自己也在为公司研究一个新的基于OOP的开发架构。但我自己也没有忘记对 PHP 的关心,有时间也是去 CU 灌灌水,帮忙回答一些问题。今天我准备把近期看到一些关于 PHP 经验、技巧的内容进行整理发布到 BLOG 上来。

一、include 与 require:

        对于一些刚刚接触 PHP 的网友,都对 include 与 require 两个命令感到很疑惑,我在最近也收到了不少网友关于这两个命令的一些问题,所以特别在此对这两个命令进行一下说明。

        include 与 require 这两个命令的功能非常的类似,它们的作用都是引入一个外部的档案,灵活使用 include 与 require 可以方便我们更好的模块化编程,比如我们可以把网站所需要的函数或一些公共程序部分写成一个独立的 PHP 程序,在当前程序需要使用到的时候,include 或 require 它,这样我们可以避免把所有的程序写在一起给以后的维护工作带来很大的麻烦。

        在这里,我主要说明一下 include 与 require 的区别,对于一个 include("test.php") 的操作,只有当程序执行到 include 语句时代码才被解析和执行,而 require("test.php") 则在 php 程序的预编译阶段就会用 test.php 的内容替换 require("test.php") 的语句。

        哪什么时候用 include ,什么时候用 require 呢?我的建议是如果被引入的外部档案调用频繁,就使用 require,因为PHP可以在通读代码的前期就可将指定档案插入到脚本中,执行速度稍快。如果被引入的外部档案是包含在一个控制块中(如:if,switch 等)就使用 include,因为如果使用 require ,即使你的程序根据 if 条件不需要引入档案,但 PHP 再预编译的时候还是会用档案内容替换 require 语句。

        补充说明:include_once 与 include ,require_once 与 require 命令类似,唯一的区别是 inlcude_once 和 require_once 对档案只引入一次,如果判断之前档案已经被引入过,将不再引入。


二、关于 PHP4 中继承类的构造函数:

        构造函数将在类的实体被定义时执行,如果类 A 继承了类 B,两个类都有自己的构造函数,那么当定义类 A 的实体时哪一个构造函数会自动执行呢?下面我们来看两个例子:

例程:1

<?php
class foo{
    function
foo(){
        echo
"foo";
        return;
    }
}

class
foo1  extends foo {
    function
foo1(){
        echo
"foo1";
    }
}
$a = new foo1();
?>


        程序的执行结果是显示:foo1 ,所以类 A 的构造函数自动执行了,类 A 所继承的类 B 的构造函数没有执行,我们把类 A 的构造函数去掉试试:


例程:2

<?php
class foo{
    function
foo(){
        echo
"foo";
        return;
    }
}

class
foo1  extends foo {
}
$a = new foo1();
?>



        程序的执行结果是显示:foo ,这一次类 A 所继承的类 B 的构造函数自动执行,所以总结如下:当一个类拥有自己的构造函数时,它将被自动执行,否则它所继承的类的造构函数将会被自动执行。

三、两个不引人注意但的确好用的 php 函数:
  • print_r()
      print_r 函数可以输出一个数组的结构:
      例如:

      $a[0] = 1;
      $a[1] = 2;
      $a[2] = array('a'=>'a','b'=>'b');
      print_r($a);

      执行结果是将会显示:Array ( [0] => 1 [1] => 2 [2] => Array ( [a] => a [b] => b ) )

      在实际编程中,因为 print_r 函数可以帮助很方便地观察一个数且的结构和键名、键值等信息,所以在调试程序时可以给我们很多的方便。

  • var_export()
      var_export 函数 输出或返回一个变量的字符串表示;

      例如:

      $a[0] = 1;
      $a[1] = 2;
      $a[2] = array('a'=>'a','b'=>'b');
      var_export($a);

      执行结果是将会显示:array ( 0 => 1, 1 => 2, 2 => array ( 'a' => 'a', 'b' => 'b', ), ) 。很显然,这是一个完整的定义数组变量的结构。

      在实际编程中,我们可以把一个组用 var_export 函数生成一个定义变量的结构保存文件中,这样被保存的文件被程序 include 后我们将直接得到一个可以使用的变量,这样比把数据放在文本中,再读取分析得到一组变量要方便得多。

      例如:


    <?
       $a
    [0] = 1;
       
    $a[1] = 2;
       
    $a[2] = array('a'=>'a','b'=>'b');
       
    $content = "<?/r/n/$a = ".var_export($a,true)."/r/n?>";
       
    $fp=fopen("a.php","w");
       
    flock($fp, LOCK_EX);
       
    fwrite($fp,$content);
       
    flock($fp, LOCK_UN);
       
    fclose($fp);            
    ?>


            以后我们只要 include_once("a.php");就可以得到一个 $a 的数组变量,在此例中 var_export($a,true) 的参数 true 表示 var_export 的结果进行返回而不会输出显示。
  • posted at 13:38:44 on 05/18/04 by Arnold - 94 views - Category: PHP

    Comments

    可否加个链接?很喜欢你的blog
    posted by vidar, 05/20/04 19:07:52
    好啊,请问你的网站名称和地址是多少?
    posted by Arnold, 05/21/04 11:14:13
    永远的PHP
    http://www.phpchina.org/blo...

    我已经加了你了,我也是CU上面的,ID叫vidarz
    posted by vidar, 05/21/04 18:47:51
    只有当程序执行到 include 语句时代码才被解析和执行,而 require("test.php") 则在 php 程序的预编译阶段就会用 test.php 的内容替换 require("test.php") 的语句。
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    并不是这样的,看看手册吧:
    require() and include() are identical in every way except how they handle failure.

    Note: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值