登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 计算机科学与技术 > 正文

基于PHP的学生个性化课选课系统的设计与实现外文翻译资料

 2022-11-06 11:11  

Useful Php tips for beginners – Part 1

By Silver Moon On Mar 29, 2012

In this series we are going to look into some useful tips and techniques that can be used to improve and optimise your php code. Note that these php tips are meant for beginners and not those who are already using mvc frameworks etc.

The Techniques

1. Do not use relative paths , instead define a ROOT path

Its quite common to see such lines :

1

require_once(../../lib/some_class.php);

This approach has many drawbacks :

It first searches for directories specified in the include paths of php , then looks from the current directory.
So many directories are checked.

When a script is included by another script in a different directory , its base directory changes to that of the including script.

Another issue , is that when a script is being run from cron , it may not have its parent directory as the working directory.

So its a good idea to have absolute paths :

1

2

3

4

define(ROOT , /var/www/project/);

require_once(ROOT . ../../lib/some_class.php);

//rest of the code

Now this is an absolute path and will always stay constant. But we can improve this further. The directory /var/www/project can change , so do we change it everytime ? No instead we make it portable using magic constants like __FILE__ . Take a closer look :

1

2

3

4

5

6

7

//suppose your script is /var/www/project/index.php

//Then __FILE__ will always have that full path.

define(ROOT , pathinfo(__FILE__, PATHINFO_DIRNAME));

require_once(ROOT . ../../lib/some_class.php);

//rest of the code

So now even if you shift your project to a different directory , like moving it to an online server , the same code will run without any changes.

2. Dont use require , include , require_once or include_once

Your script could be including various files on top , like class libraries , files for utility and helper functions etc like this :

1

2

3

4

require_once(lib/Database.php);

require_once(lib/Mail.php);

require_once(helpers/utitlity_functions.php);

This is rather primitive. The code needs to be more flexible. Write up helper functions to include things more easily. Lets take an example :

1

2

3

4

5

6

7

8

9

function load_class($class_name)

{

//path to the class file

$path = ROOT . /lib/ . $class_name . .php);

require_once( $path );

}

load_class(Database);

load_class(Mail);

See any difference ? You must. It does not need any more explanation.
You can improve this further if you wish to like this :

1

2

3

4

5

6

7

8

9

10

function load_class($class_name)

{

//path to the class file

$path = ROOT . /lib/ . $class_name . .php);

if(file_exists($path))

{

require_once( $path );

}

}

There are a lot of things that can be done with this :

Search multiple directories for the same class file.
Change the directory containing class files easily , without breaking the code anywhere.
Use similar functions for loading files that contain helper functions , html content etc.

3. Maintain debugging environment in your application

During development we echo database queries , dump variables which are creating problems , and then once the problem is solved , we comment them or erase them. But its a good idea to let everything stay and help in the long run

On your development machine you can do this :

1

2

3

4

5

6

7

8

9

10

11

12

13

define(ENVIRONMENT , development);

if(! $db-gt;query( $query )

{

if(ENVIRONMENT == development)

{

echo '$query failed';

}

else

{

echo 'Database error. Please contact administrator';

}

}

And on the server you can do this :

1

2

3

4

5

6

7

8

9

10

11

12

13

define(ENVIRONMENT , production);

if(! $db-gt;query( $query )

{

if(ENVIRONMENT == development)

{

echo '$query failed';

}

else

{

echo 'Database error. Please contact administrator';

}

}

4. Propagate status messages via session

Status messages are those messages that are generated after doing a task.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

lt;?php

if($wrong_username || $wrong_password)

{

$msg = Invalid username or password;

}

?gt;

lt;htmlgt;

lt;bodygt;

lt;?php echo $msg; ?gt;

lt;formgt;

...

lt;/formgt;

lt;/bodygt;

lt;/htmlgt;

Code like that is common. Using variables to show status messages has limitations. They cannot be send via redirects (unless you propagate them as GET variables to the next script , which is very silly). In large scripts there might be multiple

剩余内容已隐藏,支付完成后下载完整资料


给予初学者有帮助的php技巧—第一部分

2012年5月29日Silver Moon撰写

在这篇文章中我们将浏览一些实用的技巧和技术,这些技巧和技术能提高和优化你的php代码。值得注意的是,这些php技巧会适合初学者,并不适合已经使用过mvc框架结构的编程人员。

  1. 不要使用相对路径,而是定义一个根路径

看到这样的行为是十分普遍的。

1 require_once(../../lib/some_class.php);

这个方法有很多的缺点:

它优先搜索被定义好的且包含着php路径信息的文件目录,然后再查找当前目录。

所以很多的目录被查找。

当一个脚本被另一个保存在不同目录上的脚本包含着的时候,它的底层目录更改为包含着的脚本。

还有另外一个问题,当一个脚本正在被执行“克隆”任务时,这个脚本可能没有父目录作为工作目录。

所以拥有绝对路径是一个好的想法。

1

2

3

4

define(ROOT , /var/www/project/);

require_once(ROOT . ../../lib/some_class.php);

//rest of the code

现在这是一个绝对路径并且将一直保持恒定。但是我们能进一步的提高。这个目录/var/www/project能改变,所以我们每次都要改变它么?不是这样的,而是通过使用像__FILE__ 的魔法常量,我们的操作更加便捷。看一个相近的例子:

1

2

3

4

5

6

7

//suppose your script is /var/www/project/index.php

//Then __FILE__ will always have that full path.

define(ROOT , pathinfo(__FILE__, PATHINFO_DIRNAME));

require_once(ROOT . ../../lib/some_class.php);

//rest of the code

所以现在即使你把你的项目转向一个不同的目录,比如把项目移到一个在线服务器上,相同的代码将会执行且没有任何的改变。

  1. 不要使用命令,包括require_once和include_once

你的脚本上面可能包含各种各样的文件,比如类库、实用和辅助功能的文件等等。

1

2

3

4

require_once(lib/Database.php);

require_once(lib/Mail.php);

require_once(helpers/utitlity_functions.php);

这是相当原始的。这段代码需要更加灵活。补充一些辅助功能来更容易地包含数据。让我们举一个例子:

1

2

3

4

5

6

7

8

9

function load_class($class_name)

{

//path to the class file

$path = ROOT . /lib/ . $class_name . .php);

require_once( $path );

}

load_class(Database);

load_class(Mail);

看到有什么不同的地方么?你一定看到了。这不需要任何更多的解释。

如果你要这样去做,你能进一步的提高。

1

2

3

4

5

6

7

8

9

10

function load_class($class_name)

{

//path to the class file

$path = ROOT . /lib/ . $class_name . .php);

if(file_exists($path))

{

require_once( $path );

}

}

有很多事物能用这种方法去完成:

在多个目录中搜索相同的类文件。

改变包含着类文件的目录变得容易,而且不破坏任何位置的代码。

使用相似的功能去加载包含辅助功能的文件,html目录等等。

3.维护你的应用程序中的调试环境

在开发过程中,我们输出数据库查询结果,销毁产生问题的变量,并且紧接着一旦问题被解决,我们对变量进行评价或是删除他们。但是这是一个好主意,让一切保留了下来,并且从长远的角度看来是有帮助的。

在你的开发机上,你可以做到:

1

2

3

4

5

6

7

8

9

10

11

12

13

define(ENVIRONMENT , development);

if(! $db-gt;query( $query )

{

if(ENVIRONMENT == development)

{

echo '$query failed';

}

else

{

echo 'Database error. Please contact administrator';

}

}

然后在服务器上你可以做到:

1

2

3

4

5

6

7

8

9

10

11

12

13

define(ENVIRONMENT , production);

if(! $db-gt;query( $query )

{

if(ENVIRONMENT == development)

{

echo '$query failed';

}

else

{

echo 'Database error. Please contact administrator';

}

}

4.通过会话传送状态消息

状态消息是完成一个作业后产生的消息。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

lt;?php

if($wrong_username || $wrong_password)

{

$msg = Invalid username or password;

}

?gt;

lt;htmlgt;

lt;bodygt;

lt;?php echo $msg; ?gt;

lt;formgt;

...

lt;/formgt;

lt;/bodygt;

lt;/htmlgt;

代码看起来是普通的。使用变量来展示状态消息已经有局限性了。他们不能通过重定向的方式被发送(除非将这些消息作为GET变量传送到下一个脚本中,这种方法是愚蠢的)。在大多数的脚本中会有多样的消息等等。

最好的方式使用会话传送(即使在相同的页上)。为此在每一页上一定有一个session_start。

1

2

3

4

5

6

7

8

9

10

11

function set_flash($msg)

{

$_SESSION[message] = $msg;

}

function get_flash()

{

$msg = $_SESSION[message];

unset($_SESSION[message]);

return $msg;

}

并且在你的脚本中:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

lt;?php

if($wrong_username || $wrong_password)

{

set_flash(Invalid username or password);

}

?gt;

lt;htmlgt;

lt;bodygt;

Status is : lt;?php echo get_flash(); ?gt;

lt;formgt;

...

lt;/formgt;

lt;/bodygt;

lt;/htmlgt;

5.让你的功能变得灵活

1

2

3

4

5

6

function add_to_cart($item_id , $qty)

{

$_SESSION[cart][$item_id] = $qty;

}

add_to_cart( IPHONE3 , 2 );

当添加一个单个项目时,你使用上述功能。当添加多重项目时,你将要创建另一个功能么?不是的。仅仅使这个功能足够灵活地获取不同类型的参数。看一个相近的例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

function add_to_cart($item_id , $qty)

{

if(!is_array($item_id))

{

$_SESSION[cart][$item_id] = $qty;

}

else

{

foreach($item_id as $i_id =gt; $qty)

{

$_SESSION[cart][$i_id] = $qty;<!--

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[139626],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图