网友支招:PHP网站开发kohana框架里的几个写法


1、字符比较:

  比较适用于比较字符是否是大小写:

  程序代码

$class='Rfdsaffsadfsadfasdfsadf';
$type=($class[0]<'a')?'libraries':'helpers';
echo$type;

  程序代码

echo('大写A:');
echoord('A');
echo('<hr/>');
echo('小写a:');
echoord('a');

  输出是:大写A:65小写a:97

  1、这样的比较应该是先转成ASCII比较的;

  2、第字符是中文,用$class[0]这样的方式就取不出来了!

  3、$class[0]这样的方式是不推荐使用的,我有点忘了,应该是4.0以前的写法,应该推荐用$class{0}

  4、比较中文时,可以用ord(mb_substr($class,0,1,'utf-8'))>127,这里是取第字符再转成ASCII,再比较,大于127的,可能认为是中文;

  2、首字母大写

和上边有点关系,也记一下;

  //Makeastring'sfirstcharacteruppercase

  程序代码

  ucfirst()

  手册里的例子是这样的:

  程序代码

<?php
$foo='helloworld!';
$foo=ucfirst($foo);//Helloworld!
$bar='HELLOWORLD!';
$bar=ucfirst($bar);//HELLOWORLD!
$bar=ucfirst(strtolower($bar));//Helloworld!
?>

  3、自动加载

  之前一直好奇,类文件还没有require进来呢,怎么就能直接可以用了呢?

  //函数似乎是5点几以后才支持的;

  之前的不支持;

  spl_autoload_register(array('Kohana','auto_load'));

  //然后kohana里可以写成类似的:

  程序代码

finalclassKohana

{
  publicstaticfunctionauto_load($class)

  {
    require$class.'.php';
  }
}
这样,你newAbc();只要Abc.php文件存在,就会直接require进来;

  看一眼应该能明白是什么意思吧


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3