search
HomeBackend DevelopmentPHP Tutorial用Zend Encode编写开发PHP程序_PHP

用Zend Encode编写开发PHP程序_PHP

Jun 01, 2016 pm 12:38 PM
encodezenddevelopimplementprogramwritecompile

Zend Encode的工作原理

  使用PHP的人都知道,它是一个脚本编程工具,用它写的程序,必须以源码的形式放置在Web服务器上,所以我们无法保护自己的源代码。大家都知道任何一个脚本程序的执行效率同具有相同功能的编译好的二进制代码相比较,它的执行效率都是比较低的。那么要是有一个工具能够帮我们把用PHP写的程序编译成二进制代码就好了,这样不但执行效率提高了,
运行速度也加快了。真要是有这么一个工具,那就是一举两得了。

  现在这不是梦想了,Zend Encode就是为此而开发的,它可以直接将脚本编译成二进制码。有了Zend Encode,你就可以将自己写好的PHP程序,编译后分发给很多用户,而用不着公开自己的源程序代码。编译好的二进制代码可以被Zend Optimizer透明读取,也就是说,客户只要在他的服务器上安装Zend Optimizer就可以执行由Zend Encode编译好的PHP程序。编译程序中包含有Zend Optimizer的部分代码,所以编译过程中对程序代码进一步作了优化处理,这即意味着脚本的执行效率提高了。

  从一定意义上讲,Zend Encode是一个“PHP编译器”。但是,它又不是一个真正意义上的编译器,因为真正编译完成的程序可以脱离原来的编译环境运行,而Zend Encode编译过的程序,需要有Zend Optimizer的支持。就像编译好的Java二进制代码,需要JVM的支持。所以,Zend Optimizer可以看作是PHP编译好代码的虚拟机。不管怎么说,它们要通过相互配合使用。

  目前Zend Encode支持的操作系统有:Solaris、Linux、FreeBSD及Windows。Zend Encode可以直接运行,电脑系统中不一定非要安装PHP。

  Zend Encode的安装

  先去下载一个软件包吧!Zend Encode不是自由软件,使用它要付费,而且价格相当高。幸好zend.com提供了一个可供试用的软件包,用户可以免费试用30天。这个软件包可以直接从www.zend.com上获得。所以,首先要到www.zend.com上下载Zend Encode、Zend Optimizer软件包。其次,要下载一个授权文件license。由于Zend Encode是一个授权使用的产品,所以需要用户从zend.com上申请一个license。申请步骤如下:

  要申请一个试用的license,需要向zend.com提供你正在使用的计算机的ID,也即在申请页中填写host ID(实际上就是你计算机上的网卡的MAC地址)。查看计算机ID的方法如下:从zend.com下载一个lmutil.z的程序,解压后得到程序lmutil,运行它,它会根据系统的硬件特征产生一个序列串。将这个序列号填入到申请license页的host ID中,zend.com会在48小时内为用户生成一个license,下载此license文件,文件名为zendEncode.dat,它只能在这台电脑上使用。

  1.将Zend Encode软件包也解压缩到/usr/local/Zend目录下。解压缩完成后,目录下多了一个zendenc的文件,它就是那个“编译器”啦。

  2.将那个license文件复制到/usr/local/Zend目录下安装完成。

  Zend Optimizer的安装

  完成了Zend Encode的安装,才完成了一半任务,要使用编译后的PHP二进制代码,还要安装一个解释器——Zend Optimizer,有了它的支持,编译后的PHP二进制文件才能被正确地执行。

  与Zend Encode不同,Zend Optimizer是一个免费软件,它的主要功能是加速PHP脚本文件的运行。据Zend.com称,有了Zend Optimizer的优化,程序的执行效率可以提高600%,经过笔者的简单测试,执行效率的确是提高了不少。

  安装Zend Optimizer步骤如下:

  1.解压缩Zend Optimizer软件包,将zendoptimizer.so文件复制到/usr/local/Zend/lib目录中。

  2.打开/usr/local/lib/PHP.ini文件,在文件中加入以下两行:

  zend_optimizer.optimization_level=15

  zend_extension="/usr/local/Zend/lib/ zendoptimizer.so"

  3.重启动Apache服务器,使以上更新生效。

  Zend Encode的使用

  现在准备工作全部完成了,我们写一个简单的PHP脚本,使用Zend Encode编译一下,看看效果如何。先写一个最简单的脚本,看看编译后的代码能不能执行:

  #vi test.PHP

  <? Phpinfo(); ?>

  编译它:

  #[root@mail Zend]# ./zendenc test.PHP testencode.PHP

  Zend Encoder Unlimited (TEST DRIVE) v1.1.0 (c) Zend Technologies, 1999-2000

  Licensed to: xqkred.

  Compiling test.PHP...

  Done encoding test.PHP.

  Optimizing... Done.

  Saving... Done.

  好,成功编译。不过,编译好的程序大小较之以前要大很多。

  将testencode.PHP复制到Web服务器的发布目录下,在浏览器上键入localhost/testencode.PHP,哇!编译后的代码可以成功运行了!由于我们使用是试用版的Zend Encode,所以,在页面的最上面会出现一个图片,说明这是一个由Zend Encode试用软件包所产生的二进制文件。正式版的软件中,图片将不会再现。

  下面再看看它的执行效率吧!首先写一个小的计算程序粗略估计一下:

  [compute.PHP]

  <?

  r=time();

  for( 121=0;121<1000000;121 ) {

  if((121 )!=0) {echo 121; echo ",";}

  else { echo "<br>";}

  }

  =time();

  echo "<br>"; echo "It used:"; echo -r; echo " seconds";

  ?>

  这个程序在执行时,取系统的时间,完成后再取系统时间,两个值的差即为整个程序运行所需时间,先在没有编译的情况下执行一遍,然后用Zend Encode编译后再执行一遍。比较结果:没有编译的情况下,运行所需时间平均为19秒,编译后的代码平均执行时间为9秒,看来执行效率是提高了不少。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools