Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding PHP library installation

I'm trying to understand the options and terminology relating to "installing" PHP libraries on a system (Linux/OSX)

Here are some specific points I'm trying to cover:

  1. What does "install" mean when relating to PHP libraries?
  2. What's the difference between libraries and extensions?
  3. How does php.ini fit in?
  4. How does PEAR fit in?
  5. How do you import/include libraries?
  6. If I create my own library, what's the best way to package and distribute it?

Thanks- and sorry for the multi-part...

like image 579
Yarin Avatar asked Dec 09 '10 18:12

Yarin


People also ask

How do PHP libraries work?

A: PHP library is a package of code that is meant to be reused by many programs. Libraries provide you with tools you might likely need when writing an application. To reduce development time, developers can use PHP libraries instead of writing code to add features to the website.

How do I know what PHP extensions are installed on Windows?

PHP extensions are usually called "php_*. dll" (where the star represents the name of the extension) and they are located under the "PHP\ext" folder.

What does it mean to install PHP?

PHP Installing means either manually compiling the source code ( common if you're setting up a customized PHP on the server ), or more practically downloading packages such as php5 , php5-common and related packages with a tool such as yum or apt-get ( at least on Linux ).


1 Answers

A couple points...

PHP has no native "import" infrastructure, like python, java, or .net. There are several ways that libraries can be used in PHP.

  1. compile them into the PHP binary. This is the most advanced way, and not usually desirable unless you have very special needs.

  2. Install them as PHP modules on the server, and include them in PHP.ini. From the point of view of the PHP programmer, these extensions are part of PHP -- always available. It's just easier to add and remove them without having to rebuild PHP itself.

  3. Install PHP code on the server somewhere, and include() it into your PHP script.

  4. Save a copy of a library into your project, and include it into your PHP script.

--
At a basic level, code is either part of the interpreter (static or dynamic), or it is plain old PHP code that is include()ed into your project.

For your purposes, I can only suggest that you stick with an industry standard PHP distribution (pick a good linux OS, and use it's PHP). Then almost all the libraries you will need at the interpreter level are available as add-on packages, and the complexity of that is left up to those who do it every day.

On RedHat/Centos, you might run:

yum install php php-memcached php-gd php-pecl

--
Regarding all the other kinds of libraries that you might want to use, it's most likely best to adopt a good PHP framework which takes care of all that for you.

Some examples are:

  1. Zend Framework
  2. CakePHP
  3. Codeigniter
  4. http://www.phpframeworks.com/
  5. Etc...

(not in any order, just ones that came to mind)

Provided that you have taken the standard approach of using RPM's or similar to manage the compiled in aspects of PHP and extensions, then a good solid framework will take care of the inclusion of all your additional PHP library code you need.

What the end result is, is that you focus on delivering a product, and not on all the infrastructure that you would otherwise have to learn and invent.

--
php.ini is parsed and run when PHP starts (each time for command line, once per server start in apache). It defines a lot of settings, includes a lot of modules, configures those modules, etc...

You can actually override some settings in php.ini with the ini_set() function in PHP. However, this is only effective for some settings. Others need to be set before your script starts.

When running under apache, you can add lines to .htaccess and <VirtualHost> directives which totally override PHP.ini for that directory/virtual host.

(please correct my syntax and remove this note if it is wrong)

<VirtualHost *>
    ServerName www.example.com       
    DocumentRoot /home/joe/site/docroot
    php_value include_path "/home/joe/site/php-code"
</VirtualHost>

--
In response to your #6 question about your own library and the best way to package it, I suggest you first evaluate the need of the library. And if you really are on to something, then find out the most common way people are doing it. If it is a simple library, then a .php file with a nice website would be sufficient.

--
Maybe a bit rambling, but I hope this points you in the right direction.

like image 138
gahooa Avatar answered Sep 23 '22 23:09

gahooa