navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

Up and Running with PHP: PEAR, PECL, and Composer

Up and Running with PHP: PEAR, PECL, and Composer

with Joseph LeBlanc

 


Instead of downloading PHP code and figuring out how it fits into your project structure, use PEAR, PECL, and Composer to do it for you. Composer manages libraries for your PHP project, PEAR installs packages for your entire server, and PECL compiles extensions directly into PHP itself. In this course, author Joseph LeBlanc demonstrates how to find, install, update, and remove packages with this suite of invaluable PHP tools.
Topics include:
  • Installing on Mac and Linux
  • Discovering channels and packages with PEAR
  • Coding with PEAR packages
  • Starting a new application with Composer
  • Installing and maintaining packages with Composer
  • Finding and installing PECL extensions

show more

author
Joseph LeBlanc
subject
Developer, Servers, Programming Languages, Web Development
software
PHP , PECL , PEAR , Composer
level
Intermediate
duration
1h 0m
released
May 30, 2013

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00 (music playing)
00:04 Welcome to Up and Running with PHP, PEAR,
00:06 PECL, and COMPOSER. I'm Joe LeBlanc, and in this course,
00:10 we'll look at defining dependencies for a new project with COMPOSER.
00:15 Then we'll use a generated auto-loader to initialize objects.
00:19 Next, we'll install PEAR and PECL on Windows, MAC-OX 10, and Ubunto Linux.
00:27 Then finally, we'll maintain installed PEAR and PECL extensions.
00:31 PEAR, PECL and COMPOSER are waiting to install all the packages you need for
00:35 your next PHP project. So let's get up and running.
00:38
Collapse this transcript
What you should know
00:00 To get the most out of this course, there are a few things you'll want to be
00:03 familiar with. First, there's PHP.
00:06 You'll need to install PHP and have it ready on your system.
00:09 To learn more about installing PHP, check out installing Apache, MySWQL, and PHP
00:14 with David Gassner. In addition to installing PHP, you'll
00:18 also want to know how to use it. For that, have a look at PHP with MySQL
00:22 Essential Training, with Kevin Skoglund. Finally, PEAR, PECL and Composer are
00:28 command line applications. Since you'll be working with the command
00:31 line, you'll want to be familiar with it. To learn more about the command line on
00:35 Mac-OS X, have a look at Unix for Mac-OS X Users, with Kevin Skoglund.
00:40 PEAR, PECL and composer are all based on PHP.
00:44 Since their command line tools, the more comfortable you are with the command
00:47 line, the easier they'll be to learn.
00:49
Collapse this transcript
Using the exercise files
00:00 This course includes exercise files you can use to follow along.
00:03 The exercise files are designed so you can jump into the code used for a
00:07 specific video. The exercise files are organized by chapter.
00:11 Inside each chapter, there's a folder for each video that has exercise files.
00:16 This is a folder for chapter 1, video 6. There's a start folder, a final folder
00:22 and a snippets.txt file. The start folder has the code that I'm
00:27 using at the beginning of the video, and then the final folder has the final code
00:31 at the end of the video. Snippets.txt has code that's used in the video.
00:37 Some videos do not have a Snippets.txt files, and other videos just have a final folder.
00:42 With the exercise file structure in mind, you can now watch any video in this
00:46 course and follow along.
00:48
Collapse this transcript
Understanding PHP package managers
00:00 The PHP community provides several tools for managing extensions and packages.
00:04 This can be a it confusing, as the correct way to approach them is not
00:09 always self-evident. Let's take a look at what tools are
00:11 available and when you would want to use each of them.
00:14 But first, let's have a history lesson. The first tool to come out was PEAR and
00:19 it's designed for systemwide packages. Anytime you install a package through
00:24 PEAR, it's going to install it into a central location.
00:27 This way you can use that package in any PHP script on your computer.
00:33 Using PEAR typically requires root or administrator privileges, many times
00:38 you'll need to supply your password before using PEAR.
00:42 In addition to installing packages, PEAR can also install commands that call those
00:46 packages, these commands are available on the command line.
00:50 After PEAR was released PECL came along. PECL is designed for compiled extensions
00:56 to PHP. The PECL tool is based on the PEAR tool.
01:00 When you compile a PECL extension, it creates a shared object.
01:04 This shared object is loaded into PHP at runtime.
01:08 PECL is often used as a testbed for new core PHP functions.
01:13 This way you're able to get new functions into the PHP run time without modifying
01:17 PHP itself. Finally, the PHP community created Composer.
01:22 Composer is used to list project dependencies.
01:27 It's similar to NPM in node. And Bundler in Ruby.
01:31 The Composer tool helps you list out dependencies and install them.
01:35 Composer can also be used to load local copies of PEAR packages.
01:39 These local copies do not interfere with the centrally installed PEAR packages.
01:43 So where are PEAR, PECL, and Composer today?
01:48 There are some PHP projects that still depend on PEAR packages.
01:51 When you go to install a PHP project that needs a PEAR package, it will tell you
01:56 which one it needs, so you can go and install it.
01:59 Many software bindings are available through PECL.
02:02 There are projects such as Varnish and Memcache that use PECL extensions to
02:07 communicate with PHP. Composer is the best choice for self
02:11 contained projects. It leaves no traces and no one composer
02:15 project interferes with another one that's on the same computer.
02:19 Knowing a bit about PHP's package and extension managers can help you pick the
02:23 right tool quickly. Consider using Composer when your
02:26 starting a new project or adding dependencies to an existing one.
02:29 Otherwise, PEAR and PECL are available for the projects needing them.
02:34
Collapse this transcript
1. Exploring Composer
Installing Composer
00:00 Composer's distributed separately from PHP as a stand-alone executable FAR file.
00:06 Let's install this file and put it in one of our command line paths.
00:09 Before downloading Composer make sure you have Apache, MySQL and PHP set up on your computer.
00:15 I've set up Apache, MySQL, and PHP according to the directions in Installing
00:19 Apache, MySQL and PHO with David Gassner. Specifically, I went to chapter two, and
00:25 watched, activating Apache on Mountain Lion, and also, activating PHP on Mac OS X.
00:31 With PHP and Apache ready to go, let's install composer.
00:35 Go to getcomposer.org and click Download. There are two terminal commands presented
00:41 for installing composer. The first one is based on curl and the
00:45 second one is based on some PHP code. Let's see if we have curl installed.
00:52 We do have curl installed, so we can use curl.
00:54 Copy this inside a line, and then Paste it into terminal.
00:58 This is going to download in installer's script and then feed it through PHP.
01:03 The composer and installer scripts checks our PHP installation to make sure that
01:07 everything is ready to go, if there are any problems its going to alert us.
01:11 In this case, we have a problem we need to add detect unicode equals off to the
01:17 end of our PHP INI file, so lets do that now.
01:20 Type in sudo, and then pico, which is a text editor, and then Copy the path to
01:27 the php.ini file. Paste that in, and then Copy the PHP INI setting.
01:34 Now hit ENTER, and then go to the end of the file.
01:38 To do this in pico, hold down Ctrl and press V.
01:41 Now, that we're at the end of the file, Paste in the setting.
01:45 Finally, press Ctrl + X and then type Y for yes to save the changes.
01:51 Hit Return to accept the file name. And then, you'll be taken back to the
01:55 Terminal Command Prompt. So now our PHP installation is ready for Composer.
02:00 I'm going to press the Up key two times, and that'll get us back into the command
02:04 for downloading the installer script. When I hit Return, it begins downloading Composer.
02:10 Composer is now downloaded to my computer.
02:12 If I type in ls, you'll see composer.phar at the bottom.
02:17 I can run Composer by typing in PHP and then composer.phar.
02:22 Now, typing in PHP composer.phar every time isn't very convenient if you're
02:26 going to be using it in other places in your file system.
02:29 So, let's move the composer.phar file into palace where it will be more convenient.
02:34 I'm going to move composer.phar into user local bin.
02:37 This is a path that's in our command line path, so I'll be able to access it from
02:42 any point in the file system... To do this, type cp then composer.phar
02:48 and then a space and then /user/local/bin.
02:54 It's giving me permission denied, because user local bin is a protected folder.
03:00 To get around that, I'm going to press the Up key, and then go back to the
03:04 beginning of the command and preface it with sudo.
03:08 If it asks for your password, be sure to enter it.
03:11 Now, I can type it composer.phar and it will run the command.
03:15 This can also be set up so that we can just type composer instead of composer.phar.
03:20 To do that type in mv for move and /usr/local/bin/composer.phar, then type a
03:30 space, then type /user/local/bin/composer.
03:38 Again, this is a protected directory, so we're going to start this command with sudo.
03:43 Now, just type composer. Composer is now set up and ready to go on
03:47 this computer. When you want to set up composer on a new
03:50 computer, just use the insulation script. It'll make sure your PHP configuration is
03:55 suitable for Composer, then download the latest copy.
03:58 Once, Composer is downloaded, move it to a place within your command line path.
04:01
Collapse this transcript
Installing Composer on Windows
Collapse this transcript
Starting a new application
00:00 Once composer is installed on your system you can use it to start managing your
00:04 project decencies. Let's start a new project.
00:07 If I started using composer here it would begin to download decencies into my Home
00:11 folder and I don't want that. What I really want to do is start with a
00:15 fresh folder, so first let's create that folder...
00:19 Type in m-k-d-i-r, and then space, and then website.
00:24 Now that we have that folder, let's change directory into it.
00:27 So type c-d space website. So now we have a fresh, blank folder,
00:33 ready for our project. The first thing we're going to do is type
00:36 in composer And then a space and then we're going to pass in a command in composer.
00:41 And the command we are going to use is called init.
00:43 This command helps us set up a new composer.jsound file that allows us to
00:48 define the dependencies that we want to use for this project.
00:51 After typing in composer init it walks us through building the composer.jsound file
00:57 ineractively The first few questions are going to pertain to identifying the project.
01:02 The first thing it's going to ask us for is a package name.
01:05 Now this is important if we're going to share this package with other developers
01:09 in the PHP community. But, at the moment, this is just going to
01:12 be a private project, so we don't really need to pay attention too much to the
01:15 package name here. We can accept the default that it's
01:18 suggesting as root/website. So just type Enter and it'll use that.
01:24 Next, we can type in a description. I'm just going to type our website.
01:29 After that, it's going to ask for an author.
01:31 Type in your name and then follow that by your email address in angle brackets.
01:36 Next, it's going to prompt for the minimum stability.
01:42 Since we're not sharing this project with anyone else we can just leave this blank.
01:47 And then finally it asks for a license, I'm going to choose MIT.
01:52 So those five questions took care of the identification portion of the composer.
01:56 (UNKNOWN) file. Next, there's another section that
02:00 defines all of the dependencies for the project.
02:02 We can add as many dependencies as we want, but in this case, we're just going
02:06 to add one. So now it's asking us if we would like to
02:09 define our dependencies interactively and the default is, yes.
02:13 So, I'm just going to hit Enter. Now, it's prompting if we want to search
02:16 for a package. I want to use a package that will help me
02:20 read markdown And translate that into HTML.
02:24 So I'm going to search mark down. So there are several packages listed here
02:29 that interpret mark down. The one I want to use is the first one on
02:33 the list. The one marked dflydev slash markdown.
02:36 That one is set as number 0, so type in number 0, then type Enter.
02:42 Next it's going to ask us to specify a version constraint.
02:46 In this case I want the very latest version of the 1x series.
02:50 So type in 1.x. This is going to pull in the latest
02:53 package in the 1.0 series. So that just added the markdown package
02:59 to our project. We can add another package here by
03:02 searching, or we can just press Enter to get out of this.
03:05 So lets press Enter, next it prompts us to define our dev dependencies.
03:10 So here we can add any packages that we want to use during development.
03:14 This would be handy for any unit testing frameworks or other tools that you use
03:19 specifically while developing the application rather than deploying it.
03:23 In this case I don't want to add any Dove dependencies, so I'm going to type no and
03:27 then enter. Finally, composer displays our complete
03:31 composer.J file. Everything looks good, just hit enter to
03:37 accept it. Now let's type in LS.
03:40 Running composer in it added exactly one file to this folder called composer.json.
03:45 Now let's use this file to install the dependencies for this project.
03:49 Type in composer space install. COMPOSER read our composer.json file, saw
03:57 the dflydev/markdown dependency listed And then also saw that we wanted the 1.x
04:04 version of the software. The latest version in the 1.x series is
04:09 1.0.2, and so it went ahead and downloaded this.
04:13 It also generated a lock file. Let's type in LS again.
04:17 You'll notice that we have a composer.json file, a composer.lock file...
04:23 And a vendor folder. All of the code for the markdown package
04:27 went into the vendor folder. Let's take a look at this composer.lock file.
04:32 I'm going to type in more composer.lock. You'll see here that the composer.lock
04:39 file recorded the version that was installed.
04:42 This ensures that if we share this project with someone else.
04:46 They'll always get the exact version of the library that we installed.
04:50 I'm going to press Q to get out of more. So now we have the markdown dependency
04:54 and it's ready to go for our project. Using Composer is as easy as typing
04:59 Composer Init. After that, Composer will step you
05:02 through creating a Composer dot JSON file.
05:04 Which can then be used to install dependencies.
05:06
Collapse this transcript
Coding with packages
00:00 Once you've started a new composer project, and downloaded your
00:03 dependencies, you can start using them right away.
00:06 Composer automatically generates an autoloader you can use to pull in your
00:09 dependency classes easily. Let's take a look at this autoloader in action.
00:14 First, let's go to the exercise files folder and pull up the website folder.
00:19 We're going to copy this website folder into the Apache document root.
00:23 So let's open up a new finder window and then rom here I"m going to go to that folder.
00:30 The path to go to is slash library slash webserver with a capital S and all one
00:35 word, and then slash documents. So now that I have this folder open I'm
00:41 going to copy the website folder into there.
00:44 When I try pasting it in, Finder alerts me and says that this is protected folder
00:48 and asks for my password, so I'm going to type it in.
00:52 Finally, I'm going to take a look at some of the files that have been added to this
00:55 project, so I'm going to drag the website folder over to sublime text.
00:59 You can use any text editor that you wish.
01:02 First, lets pull up text.md. This is a simple markdown file.
01:07 There is a header and then there is a paragraph of text.
01:11 Our index.php file is going to read text.md and turn it into markup.
01:16 So on line three we have require vendor/autoload.php.
01:21 Autoload.php is automatically generated by composer whenever we install dependecies.
01:26 This file registers all the packages that were downloaded with Composer with PHP's Autoloader.
01:32 As long as we include this required statement, once in our project, we're
01:37 able to use this Autoloader to pull in any of the classes in any of our dependencies.
01:42 This is done through PHP's name spacing. So in this case on line 5 we're declaring
01:47 a new mark down parser object. It's distributed by dflydev as part of
01:51 the mark down package. So we do \dflydev\markdown\markdownparser.
01:58 Next on line seven, we're getting the file contents of text.md.
02:03 This is going to return everything as a string.
02:05 And store it into the content variable. Finally, on line nine, we're passing
02:10 content into the transform markdown member function of the markdown parts or object.
02:15 This is going to return markup, which we're then sending to the browser.
02:19 Lets take a look at this in action. Go to your browser and then type in
02:23 localhost and then slash website. So here, the markdown got translated into
02:29 standard markup. If we do View Source, you can take a look
02:34 at the markup that got generated. We were able to generate this markup
02:38 using that markdown dependency. We didn't have to require it specifically
02:42 because we had the autoloader in place. So after downloading your dependencies,
02:46 always add composers generated autoloader.
02:48 It will let you load all of your dependencies as needed with only one
02:52 require statement.
02:53
Collapse this transcript
Listing commands
00:00 Aside from the documentation at getcomposer.org.
00:03 You can learn more about composers various commands from composer itself.
00:07 Lets have a look at composers built in help system.
00:10 So first I'm just going to type composer from the command line and hit Return.
00:17 I'm going to scroll up to the top. And you'll see that there are several
00:20 options that are available for the base composer command.
00:24 In addition to the options, it also shows the composer version that's been installed.
00:28 This hash matches up to the get repository where composers hosted.
00:35 If we scroll back down it shows all of the available commands that you can run
00:39 with composer. You run these by typing composer, and
00:42 then a space, and then the name of the command.
00:44 One of the most important commands is Help.
00:47 Help allows us to get more information about all the other commands.
00:50 Type in composer help status. You'll notice the typing in composer help
00:57 status also showed a list of the options that are relevant to status.
01:01 At the bottom, a help section specific to status is displayed.
01:05 Now, let's type in composer help require. (SOUND).
01:10 Notice that this is similar to the help that we got for the status command but,
01:14 there's some more information here. There's an argument section, that tells
01:19 us the arguments that are valid for the required command.
01:23 There also many more options that are valid for the required command, that are
01:26 not valid for the status command. For instance, the required command has
01:30 the option dev. This option allows us to add the
01:34 dependency into our development dependency section.
01:40 And then finally the help section has a summary of the command at the bottom.
01:44 For quick help with composer use help to learn more about individual commands.
01:48 Help will only show the options and information relevent to that command.
01:51
Collapse this transcript
Updating the lock file
00:00 When you install your project's dependencies, Composer generates a lock
00:03 file to record the exact versions downloaded.
00:06 This is great for maintaining consistency when you're sharing your project with
00:09 other people, but newer versions aren't going to get installed.
00:13 To install newer versions, the Update command must be used.
00:17 Let's take a look at the Update command in action.
00:19 Go to the exercise files and then pull the website folder into Sublime Text.
00:24 At the moment, we have a composer.json file as well as a composer.lock file.
00:30 On line 5 of composer.json, we have the slim library installed.
00:34 In this composer.json file, we specified version 1.6.0.
00:39 If you take a look at composer dot lock, on line six, 1.6.0 is also set as the
00:45 version of Slim to use for this project. Let's go back to Composer dot JSON.
00:51 Let's say we wanted 1.6 with Slim, but we wanted the latest patch for version 1.6.
00:57 To get that, we'd need to change the 0 to an x.
01:00 Now let's save composer.json and let's go to the command line to attempt to install this.
01:07 I'm going to change directory to the exercise files.
01:09 Let's type in cd and then a tilde then slash, and then Desktop with a capital D
01:16 and then slash ExerciseFiles with a capital F, then slash, and then zero one,
01:23 slash zero five, slash start. And then slash website.
01:29 Now I'm going to try typing composer install.
01:33 You'll notice we got this message, saying warning, the lock file is not up to date
01:38 with the latest changes in composer dot jso.
01:41 What happened is Composer read the Composer.JSON file and saw 1.6.x for Slim.
01:47 However, Composer dot lock is overriding this.
01:52 Let's use the Composer dot update command to regenerate the lock file.
01:56 You'll notice first that the 1.6.0 version of slim was removed.
02:04 Next the 1.6.7 version of slim was installed.
02:08 1.6.7 was the latest version in the 1.6 branch of slim at the time of this recording.
02:14 Now let's go back to our project and take a look at the lock file.
02:18 You'll notice now that the lock file is set to 1.6.7.
02:21 The composer.lock file records and enforces the exact version of each
02:25 dependency as they're installed. To override the lock and get updated
02:29 versions use composer update.
02:31
Collapse this transcript
Including PEAR packages
00:00 If you need a PEAR package, but don't want to install PEAR itself, it's
00:03 possible to include the package through composer instead.
00:06 Lets modify the composer.jso file to include a PEAR package.
00:11 First lets go to the exercise files and move our project into the web root.
00:15 The website folder is the folder that we want to copy, so copy it, and then open
00:23 up another Finder window and then let's go to the website route.
00:28 Go to the Go menu and then select Go to Folder.
00:31 So we want to go to slash library slash web server slash documents.
00:37 The capitalization here is important. Once you're at that location paste in the folder...
00:44 If the folder is password-protected, enter your password here.
00:47 Now I'm going to open up this folder inside of my text editor.
00:53 First, let's open up composer.json. On line 5, we already have slim listed as
00:58 a dependency. However, we'd also like to add a
01:01 marked-down parser that's available through pair.
01:04 Lets go to the snippet file and add some code here, first there is a new section
01:10 that we need to add to compose our dot Json.
01:12 Copy everything from line two to line seven and then paste it as a new section
01:17 of composer dot json I am going to add it just beneath line three.
01:24 By default, Composer is going to look for packages on the packages.org website.
01:28 By adding this repository section we can tell Composer to look in other places as well.
01:34 In line six we're defining this as a pear repository.
01:38 And then one line seven we're giving the URL for the pear repository we want to read.
01:42 In this case, we're reading the Pear2 repository.
01:46 Now let's add the package that we want to read from the pear2 repository.
01:51 Copy line nine form the snippets.txt file.
01:55 Now go to line 11 in composer.json and add a comma at the end.
01:58 If you don't add a comma at the end here, the json file will not parse correctly.
02:05 Press enter and then paste in the new line.
02:09 Notice the structure of the URL. It begins with pear and then is followed
02:14 by a hyphen and then the rest of the URL follows.
02:17 This tells composer that we're using a pear repository and then we're giving the
02:22 URL the pear repository we want to use. Finally, that's followed by a slash and
02:27 then the name of the pear dependency. At the end of the line we are telling
02:32 composer to download the very latest version, now lets save this file and now
02:37 lets try and installing this dependency. I am going to change directory into this
02:42 folder, so I am going to type in cd/Library/WebServer/Documents Then I'm
02:53 going to type cd, space website to get into the specific project folder.
02:57 Now I'm going to type composer install, now notice we get an error.
03:02 It says that the lock file is not up to date, we need to run composer update so
03:08 that the lock file gets cleared. After I typed composer update it
03:12 initialized a Pear repository at Pear 2 dot PHP dot net.
03:16 The first time you run this command it may take a few moments to complete the initialization.
03:22 Finally, the mark down library was downloaded.
03:24 Let's go back to our project. We have a mark down file that has some text.
03:31 And then we have an index.php file. Just as in previous composer projects, we
03:37 have include vendor autoload.php on line three.
03:41 Composer generated in autoloader that has registered the pair libraries in addition
03:46 to our other composer libraries. On line five, we're declaring a new mark
03:51 down main object. The name spacing for pair libraries is
03:55 registered in the auto-loader that composer generates.
03:58 The first segment is the name of the pair repository, which in this case, is pair two.
04:02 The second segment is the category for the package.
04:05 This is determined by the package name. And then finally, the third segment is
04:10 the name of the class that we're wanting to use.
04:12 Now this is working for the mark down package, but you may run across some
04:16 older packages that do things differently.
04:18 Finally, we read text.md, and then store the results in the content variable.
04:24 And then finally on line eight, we're passing content into the transform method
04:29 of the mark down object. Let's load this in the browser and see
04:32 the result. I'm going to localhost slash website.
04:37 You'll notice that the rendered mark up appears here.
04:40 If I view source, you'll notice that we have an h1 tag as well as a paragraph tag.
04:47 This was generated from our markdown file.
04:49 If you want to include a pair package in your project you can do so through
04:53 composer without having pair installed. Going through this route will install a
04:57 copy of the package for your local project without interfering with any
05:00 global copies installed with the pair command line tool.
05:04
Collapse this transcript
2. Installing PEAR and PECL
Choosing an installation process
00:00 The good news is that when PHP is compiled PEAR and PECL are automatically installed.
00:05 The bad news is that many operating system vendors distribute PHP without them.
00:10 If you're not aware of how PHP is installed on your system you can run into
00:13 a lot of confusion when you go to get them back.
00:16 Let's have a look at the most common setups.
00:19 The ideal situation is that PHP, PECL and PEAR are already installed.
00:23 To test this you can type pear or pecl on the command line.
00:30 (SOUND) In this case, I typed pear on the command line and it's telling me that it
00:33 cannot find pear. However, if pear is successfully
00:37 installed, you should get a description of the pear commands.
00:40 If PHP, PECL and PEAR are not already installed on your system, you may be able
00:45 to get them through your operating systems package manager.
00:48 Frequently, PHP, PEAR, and PECL are distributed through separate packages in
00:54 a package manager. The best way to approach this scenario is
00:57 to install PHP first, and then install PEAR and PECL.
01:01 If your operating system does not have a package manager or the package manager
01:05 does not have a package for PHP, another option is to compile PHP from source.
01:10 Whenever you compile PHP from source, it automatically installs PEAR and PECL.
01:15 The downside of this approach is that you must have a compiler on your system.
01:19 To learn more about compiling PHP from Source, check out PHP 5.4 new features
01:24 with Drew Falkman on the Lynda library. Specifically check out installing PHP 5.4
01:30 on Mac. This video has the directions for
01:33 compiling PHP from source, and should work on Mac or Linux.
01:37 If compiling PHP on your system is not possible, XAMPP is another alternative.
01:42 XAMPP is a package that includes PHP, PEAR, PECL, and other pieces of software.
01:49 To use the copies of PEAR and PECL that are shipped with XAMPP, you'll want to
01:53 use the environment variables of your operating system to get it setup.
01:57 Also, you'll want to be mindful of existing installations of PHP on your computer.
02:02 Those installations may already be in your command line path.
02:05 And you'll want to make sure that you're operating system is looking at the right one.
02:09 If all else fails, you can use the Go-Pear script.
02:12 The Go-Pear script assumes that PHP is already installed and is set up with your
02:17 binary path on the command line. It's a cross-platform script that should
02:21 work on Mac, Windows, and Linux. If you don't already have PEAR and PECL
02:26 installed on your computer, have a look through the next few videos.
02:29 In each case, the simplest method for each operating system is presented.
02:34 However, it maybe possible to use the method for one operating system on
02:37 another if your situation requires it.
02:39
Collapse this transcript
Installing on OS X with go-pear
00:00 PHP is pre-installed on OS 10, however PEAR and PECL are not.
00:05 Let's use the go-pear script to install PEAR and PECL on a Mac.
00:09 First go to pear.php.net, then click on the link for installing PEAR on your system.
00:14 From the table of contents choose Getting and installing the PEAR package manager.
00:18 Scroll down and you'll find a link for the go-pear.phar file.
00:22 Click on this link and it will download. Once the phar file is downloaded, you
00:29 want to move to home directory on your computer.
00:34 Open up a second finder window, and then click Go and Home.
00:40 Now move go-pear.phar into that folder. Next, let's go to the command line to run
00:47 the phar file. Since we're already in the home
00:49 directory, we don't need to change directory to anyplace else.
00:52 So type in php go-pear.phar. From here you can change the installation base.
01:01 I recommend doing this because you typically don't want to have things like
01:05 pear in your home directory permanently. So to change the installation base, type
01:09 1 and press Enter. Now type in /usr/local and then I'm going
01:16 to choose pear as the folder. Now most of the other paths are prefixed
01:21 with usr/local pear. This looks good to me, so I'm going to
01:24 hit Enter. Unfortunately, I need to have root
01:28 privileges to write to the User Local folder.
01:30 So I'm going to rerun this command with sudo.
01:34 It prompts for my password because it's a protected directory.
01:39 And now I'm going to change the installation base again.
01:41 Type in 1, then specify usr/local/pear, now type Enter.
01:50 The script tells us that the include path to find in our current php.ini file does
01:55 not include the PEAR php directory we just specified.
01:58 We can let the insulation script alter our php.ini file right now to fix this.
02:03 Just type and Enter. The current include path, configured
02:07 directory, and php.ini path all look good.
02:10 So I'm going to press Enter to continue. So now Pear is installed on this computer.
02:16 However if I type pear it says the command is not found.
02:20 This is because the binary folder is not in our current command line path.
02:24 Let's set up the command line so it can see the pear binary.
02:26 Go to the Exercise Files and open up the snippets.txt file.
02:34 We're going to be creating a new file in the home directory called .bash_profile.
02:41 You may already have this file on your computer.
02:42 Type in pico.bash_profile. If you already have this file on your
02:51 system, you'll already see some commands listed here.
02:54 Otherwise, Copy and Paste this line into the file.
02:59 If you have a line where path is already set, add this path starting at user local
03:04 pair bin and the colon before the rest of your path variable.
03:09 Now with the export command in place, type in Control+X.
03:13 Type in a capital Y to confirm the changes, and then Confirm the file name
03:17 to write, and press Enter. Now we need to restart terminal so that
03:21 the new path takes affect. Quit terminal, and now reopen it, now
03:27 type pear. We can also type pecl, and the same list
03:30 will come up. Pear and pecl are now installed on this
03:34 Mac and ready to go. The go-pear script downloads and
03:37 configures both of them appropriately. If you run across a computer that doesn't
03:41 have PEAR or PECL installed the go-pear script is a reliable way of getting them.
03:45
Collapse this transcript
Installing on Ubuntu Linux through apt-get
00:00 Before we install PHP, PECL and PEAR, we need to update apt-get.
00:04 apt-get has a list of all the packages that are available through the system.
00:09 And that needs to be updated first. We need to do this as a root user.
00:12 So, I'm going to preface this command with sudo.
00:16 Now, I'm going to type apt-get and update.
00:22 It prompts me for the password, so I'm going to type it.
00:27 Now, all the lists for the packages are up to date.
00:30 Now, let's install PHP. Again, using sudo, type in apt-get, and
00:37 then install. And then, php5- fivedev.
00:42 The php5-dev package is the one that we want to install because it will allow us
00:47 to use Pear and PECL on top. I'm going to hit return to continue.
00:52 And now, php5dev is going to install. If I try typing in PHP on the command
01:00 line it gives me an error. That's because the command line interface
01:03 for PHP is distributed separately on a Ubuntu Linux.
01:07 Let's install the command line interface now.
01:13 Now that we've installed the command line interface, I'm going to type PHP again.
01:18 And it sends me into the interpreter. I'm going to press Ctrl+C to get out of this.
01:23 If I try to type in pear, it gives me an error.
01:26 Now that we have PHP installed, I can install PEAR as well.
01:30 Type in sudo apt-get install, and then php-pear.
01:35 Now the PEAR's installed, I can type PEAR, and a list of commands will come up.
01:43 PECL is installed as well. So, type in pecl, and then press Enter.
01:47 And the list of PECL commands comes up as well.
01:50 If you're going to be using PECL, there's one more package you'll want to install.
01:54 It's the build essential package for Ubuntu Linux.
01:57 This package has a compiler, as well as some other tools for helping you build
02:01 PECL extension. So, to install that, type in sudo
02:05 apt-get, and then install, and then build-essential.
02:14 Type Enter to continue and the compiler will install.
02:17 PEAR and PECL are now installed on this copy of Ubuntu Linux.
02:23 If the operating system you use has a package manager, it's typically the
02:26 easiest way of installing PHP, PEAR, and PECL.
02:30
Collapse this transcript
Configuring PECL extensions on Windows
Collapse this transcript
3. Using PEAR and PECL
Installing PEAR packages
00:00 Loose Pear packages can be installed with one command.
00:03 After installing a pear package, it's available on the required path for any
00:07 PHP application. Lets install Pear package now.
00:10 First, lets take a look at the pear website.
00:12 Pear website is available at Pear.php.net.
00:15 There's a search function here at the top, where you can search for packages
00:19 that you want to install in your system. I've already found the HTTP_Request2
00:23 package there is a specific page for this package that provides some information
00:27 about it. As well as some links to download and to
00:30 read the documentation, however instead of downloading this package through the browser.
00:34 We are going to install it through the pair command line tool, lets pull up terminal.
00:40 First, let's list all of the Pear packages to make sure we don't already
00:43 have this package on our system. Type in pear list, so you'll notice we
00:49 already have a few Pear packages installed, these packages get installed
00:53 whenever you install Pear itself. Now let's install the HTTP Request2 package.
00:58 We need to install this as root, so type in sudo, then space, pear, then another
01:04 space and install. And then finally another space and HTTP_Request2.
01:13 The capitalization here is important. Since we're doing this as sudo, we need
01:17 to provide our password. The HTTP Request2 package has now been
01:24 downloaded and installed on this computer.
01:27 Let's do pear list again to see it in the list.
01:29 The HTTP_Request2 package is now listed at version 2.1.1.
01:37 We could now use this package in any of our PHP applications.
01:40 Now let's say we didn't want this package on our system anymore.also possible to
01:46 uninstall packages using Pear. Type in pear uninstall, and then HTTP_Request2.
01:53 If you don't do it as root, it's going to give you an error, because we did this
01:58 root before. So I'm going to press up on the keyboard,
02:02 go back to the beginning and then type sudo.
02:06 It gives us a confirmation saying that begun installation went okay.
02:10 Lets make sure it actually got removed. Type in pear list, you'll notice that
02:14 HTTP_ Request2 is no longer listed as one of the Pear packages.
02:18 When you install pear package its available system wide.
02:21 Those command can help you determine the versions of installed pear packages and
02:25 then the uninstall command will help you remove packages entirely.
02:28
Collapse this transcript
Discovering PEAR channels
00:00 Not all packages are available through the main PEAR Repository.
00:04 If the package you need is not available there, you must tell PEAR where to find it.
00:08 Let's add some channels where PEAR can look for packages.
00:10 So, first we're going to install some packages from the Symfony framework and
00:14 those are being distributed outside of the main PEAR Repository.
00:17 Symfony has what's called a channel where more packages are listed.
00:21 To use this channel with PEAR we need to discover it.
00:25 To do that we are going to run the channel discover command.
00:27 This needs to be done as root. So, start with sudo, then type pear, channel-discover.
00:36 And then we're going to type in the URL for the symfony channel.
00:39 Type in pear.symfony, and notice that symfony is spelled with an f, .com.
00:44 It now tells us that the discovery of pear.symfony.com succeeded.
00:51 Let's take a look at some information about this channel.
00:54 Type in pear channel-info, and then pear.symfony.com.
01:02 This screen gives us a little bit of information about the symfony channel.
01:05 One thing to note here is the alias. When you're using other channels in pear,
01:10 the alias is used as a shortcut to the URL.
01:13 One way we can use this alias is to list all the packages available in the channel.
01:18 To do this, let's type pear remote-list and then dash c for channel and then symfony2.
01:29 This command listed all the packages that are available through symfony.
01:33 Now say we wanted to install the Validator package.
01:36 We could try and type in sudo pear install Validator.
01:43 Now you'll notice that the install failed.
01:45 That's because the validator package couldn't be found in the default pear channel.
01:49 We need to specify the symfony 2 channel. So to do that we're going to use the
01:53 alias again. I am going to press up to pull up the
01:56 command again and now I am going to add symfony2/ to the beginning of validator.
02:02 Now the validator package installs correctly.
02:06 There's also a way to combine these two steps of discovering a channel and
02:10 installing a package. We can set pear to auto-discover channels.
02:14 To do this, type in sudo pear config-set and then auto_discover and then space and
02:24 then the number 1. So now we've set pear to auto-discover
02:27 channels when we add them to the beginning of package names.
02:31 Let's install PHP Unit. It's a very popular unit testing
02:34 framework for PHP. Let's type in sudo pear install, and then pear.phpunit.de/PHPUnit.
02:46 Make sure to capitalize this properly. We were able to discover the channel for
02:53 PHP unit as well as install PHP unit all in one command.
02:57 Both PHP unit and Symfony are popular pair packages, they are published outside
03:02 the main pear.php.net channel. These can be installed once the
03:05 additional channels have been discovered. You can discover channels explicitly
03:09 using the channel discover command or you can set pear define channels automatically.
03:13
Collapse this transcript
Coding with PEAR packages
00:00 Once you've installed a Pear package it's ready for you to use in any PHP file on
00:04 your computer. Let's use a Pear package to make an http
00:07 request then read the response. We're going to use the HTTP request two package.
00:13 Let's see if this is already installed on our computer in terminal type Pear list.
00:19 You'll notice that ATTP request 2 is not available, let's install it now.
00:24 Type in sudo pear install HTTP_Request2. Be sure to capitalize HTTP and Request.
00:37 Now lets make sure that it's installed type in pear list again, you will notice
00:41 that HTTP_Request2 is now installed, the exercise files have some code that's
00:47 written to take advantage of HTTP_Request2, so lets take a look at
00:51 that code now Let's change directory to the exercise files.
00:54 I have them on my desktop. So type in cd, then a tilde, then a
00:59 slash, and then Desktop with a capital D, then a slash, then exercise files, then
01:05 03, slash 03, slash final. Slash website.
01:12 Type in LS to list the directory and you'll notice that index.php is the only
01:18 file here. Let's also open this file in a text
01:22 editor so we can have a look at the code. You'l notice that on line three we're
01:29 requiring that HTTP/request2.php file... The path to this file is already in our
01:37 PHP include path next we are creating a request variable, we are setting it to a
01:43 new instances of HTTP_Request2. The first argument is a URL that we are
01:48 going to use for the HTTP_Request and the second argument is the method that we are
01:53 going to use for the HTTP_Request. On line 10 we're setting a response
01:58 object, we're using the send method of the request object to get the response.
02:03 On line 12 we're getting the HTTP status, this is going to come back as a number.
02:10 And then finally on line 14, we're getting the header, the header is going
02:14 to come back as an array. We're passing it into print_r so we can
02:18 print out the array. Let's run this code now from the command line.
02:25 When we do a get request to example.com, the HTTP status comes back as 302.
02:31 This is a redirect. Because this is a redirect, the location
02:34 header is set. The location header tells us which URL
02:38 the browser should go to. Let's try running this code now from Apache.
02:41 To do this, we're going to copy the website folder into Apache's web root.
02:46 Open up a new Finder window and then click Go, and Go to Folder.
02:51 We're going to go to the path slash library, slash webserver, slash documents.
02:58 Now copy the website folder into the documents folder.
03:02 The documents folder is password protected, so enter your password.
03:10 Now that the website folder is inside the documents folder, we can pull it up in
03:14 our web browser. Go to local host slash website.
03:19 You'll notice that all the same information is displayed.
03:22 We can also do view source and you'll see the text representation is the same as well.
03:26 You'll notice that we are able to move the index.php file to another place on
03:30 the computer without changing index.php itself.
03:34 This is because the pare path is in PHP's include path.
03:38 One thing to note, if you ran into trouble when you tried loading this on
03:41 the Mac OS 10 server, you might have an issue with this specific package.
03:46 To resolve the issue, you might wish to turn on error reporting.
03:49 To do that, have a look at debugging PHP, advanced techniques with Jon Peck.
03:56 Specifically, watch the video, configuring PHP to report what went wrong.
04:00 Once a Pear package is installed, it's automatically available within PHP's
04:04 include path. The Pear package can then be used in any
04:08 PHP file on the computer, without specifying an absolute path.
04:11
Collapse this transcript
Installing PECL extensions
00:00 PECL extensions are compiled on your computer and loaded into the PHP runtime
00:04 through shared objects. Let's have a look at a popular PECL
00:07 extension and install it. Now before we compile any PECL
00:11 extensions, we need to make sure that there's a compile on the system.
00:15 If you're on a mac, have a look at Xcode4 Essential Training with Bill Weinman.
00:20 Specifically look at installing Xcode. Be sure to install the Command-line Tools.
00:25 Now let's go to terminal, the command to install a PECL extension is PECL Install.
00:30 PECL extensions need to be installed as root.
00:33 First let's type sudo, then type pecl install and the name of the extension
00:40 we're going to install is xdebug. So now I'm going to hit Return, and it's
00:45 prompting me for my password so I can use sudo.
00:47 Now you'll notice that it downloaded the pecl extension and tried to build it but
00:55 it failed. It's giving us a message saying that it
00:58 cannot find autoconf. Let's download and compile autoconf now.
01:03 I'm at ftp.gnu.org and I'm in the autoconf directory.
01:09 Down at the bottom of the list is autoconf-latest.tar.gz.
01:16 This is downloaded to the Downloads folder.
01:21 If we double click on it it will decompress the archive.
01:24 No let's move this folder to a place where we can compile autoconf.
01:28 We're going to need to execute several commands as root, so I'm going to type in
01:32 sudo su. This will allow me to log in as a super user.
01:37 Now I'mg king to change directory to /usr/local.
01:44 I'm going to list the contents of this directory.
01:47 Let's open up this folder and finder. Type in open and then a space and then dot.
01:55 Pull up the other finder window and then move the autoconf folder into the local folder.
02:01 It will ask you to authenticate. Type in your password, and then click OK.
02:07 Now back in the terminal, if I type LS, the autoconf folder appears.
02:12 Lets change directory into the Autoconf folder, I am going to type in cd space
02:17 then auto and then Tab, to get the rest of the folder name.
02:20 Now lets compile autoconf, type in dot slash and configure.
02:27 Now we are going to compile type make, now that is install compiling, lets type
02:35 make install. Autoconf is now installed on our computer.
02:41 Now we can try installing xdebug again, type in pecl install xdebug.
02:47 The pecl extension xdebug is now installed, however, we need to do
02:55 something else to get it up and running. At the bottom it points out that we
03:00 should add zend_extension=xdebug.so to our our php.ini file.
03:07 Let's go find the php.ini file thats being used.
03:10 Go to the Exercise files and pull up phpinfo.php.
03:13 We're going to copy this file into the web root of Apache.
03:18 Open up another finder window, then click Go and Go to Folder.
03:24 We want to go to /Library/WebServer/Documents.
03:30 Copy the phpinfo.php file into this folder.
03:36 Because this is a protected folder, you will need to authenticate.
03:41 Now let's go to the web browser and load up php info.
03:44 Go to localhost/phpinfo.php. You'll notice here, that the loaded
03:51 configuration file is private etc php dot ini.
03:56 I'm going to Copy this directory path and Open it, go back to terminal, and type in
04:02 open space and then Paste in that path. Scroll down to php.ini and then open it
04:09 with your Text Editor. Now let's scroll down to the bottom.
04:16 This is where we need to add our new setting.
04:17 Copy the zend extension setting and then Paste it into php.ini.
04:26 Now Save the file. Since it's in a protected directory,
04:29 you'll need to supply your password. There's one more thing we need to do
04:35 before ext debug will be active. We need to restart the Apache server.
04:39 Type in apachectl, and then space, and then restart.
04:44 Now let's go back to the web browser. So now, let's refresh the PHP info page
04:49 so that we can see xdebug. Hit Refresh and now search for xdebug on
04:55 the screen. You'll notice that xdebug didn't get loaded.
04:57 Let's go back to terminal. You'll notice that on this line that
05:02 xdebug.so got loaded at this specific absolute path.
05:10 We need to change the php.ini file, so that it points to the specific path
05:15 rather than the relative path. So copy in the absolute path and then go
05:19 back to php.ini. Now replace xdebug.so with the absolute path.
05:26 Save the file, then restart apache again. I'm going to hit up in terminal.
05:31 Now that I've restarted the server, I'm going to go back to PHP info and refresh.
05:36 Now if I search for xdebug it appears in its own section.
05:41 You can learn more about xdebug in Debugging PHP, Advanced Techniques with
05:47 Jon Peck. Specifically look at Chapter Two.
05:49 Extensions like xdebug are available through PECL.
05:53 If you have a compiler on your computer, you can compile these extensions for your
05:56 specific system.
05:57
Collapse this transcript
Maintaining installed packages
00:00 As patches become available, you'll want to upgrade Pear packages and PECL
00:04 extensions to get the latest fixes. Let's have a look at the commands you
00:07 could use to perform maintenance. Type in pear.
00:12 Notice these two commands, update-channels and upgrade, you can use
00:16 these commands with either Pear or PECL. Let's start with upgrade.
00:21 Type in (SOUND) sudo pear upgrade. It tells us that there's nothing to upgrade.
00:27 That's because all of our pear packages are up to date.
00:30 Let's run upgrade again, only specifying a package name.
00:33 I'm going to press up to get the last command, and then I'm going to press space.
00:38 Then I'm going to type symfony with an f. Then number 2, then slash, and then
00:43 capital V for validator. Again, we already have this package on
00:49 our system. And it's already up to date.
00:52 You can update pickle extensions the same way.
00:55 Typically, you'll want to upgrade one pickle extension at a time.
00:57 So type in sudo. Pecl upgrade and then the name of the
01:00 extension you want to upgrade. I'm going to type in xdebug.
01:01 Xdebug is already up to date. The other thing you want to update are
01:05 your channels. Let's start with pear.
01:06 Type in sudo pear update-channels. You'll notice that it not only updated
01:23 the channel for pear for PHP dot net, which is the default repository, but it
01:27 also updated several other channels. including the one for PHP unit and the
01:32 one for symfony. It also updated pecl dot PHP dot net.
01:36 PECL and Pear are using the same channel list.
01:40 So if we type in sudo pecl update-channels, it will do the exact
01:47 same thing as the pear command. The channels are used to provide a list
01:51 of all of the packages that are available.
01:54 When you want to install updated copies of your packages and extensions Use the
01:58 upgrade command for both pear and pecl. This will automatically download and then
02:02 install the latest stable versions.
02:03
Collapse this transcript
Conclusion
Getting involved with the Composer community
00:00 Composer is becoming the tool of choice for managing PHP dependencies.
00:04 There are several sites you can use to learn more about composer and to get
00:07 involved with the composer community. Let's have a look at some of them now,
00:10 the first site is getcomposer.org, this is the main site for composer.
00:15 From here you can Download Composer and get more documentation.
00:19 If you go to getcomposer.org/doc, you can navigate to all the documentation for composer.
00:24 Next, there's packagist.org. Packagist.org is the main repository for composer.
00:30 You can use this site to search for packages.
00:33 If you have PHP code that you'd like to submit to packagist, you can click the
00:41 Submit Package button. Once you log into the site, packages.org
00:45 will give you instructions for submitting your own package to the repository.
00:49 If you're interested in following the development of composer, go to the
00:52 composer GitHub page. From here you can submit issues to the
00:56 project as well pull requests with more code.
00:59 Finally, there are a couple of Google groups for composer.
01:02 The first one is composer-users. Composer-users is a group for helping
01:07 people learn more about composer and answering questions.
01:10 The other Google group is composer-dev. This group is where the development of
01:15 composer itself is discussed. To fall along with the development of
01:18 composer which should checkout the composer-dev Google group as well as the
01:22 GitHub page. From there you can submit issues, offer
01:26 poll requests, and see new features as they're added.
01:28
Collapse this transcript


Suggested courses to watch next:

PHP 5.4 New Features (1h 1m)
Drew Falkman


Git Essential Training (6h 26m)
Kevin Skoglund


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,069 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,024 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked