delorian(car,timemachine)
, which means 'inherit from car
and then from timemachine
.' As a result, if both classes had a refuel()
function, Python would pick car.refuel()
.
This situation becomes more complex when further inheritance is involved. That is, if car
inherits its refuel()
method from vehicle
, Python still chooses it. What happens behind the scenes is that Python picks the first class from which you inherited and searches it and all its parent classes for a matching method call. If it finds none, it goes to the next class and checks it and its parents. This process repeats until it finds a class that has the required method.
The Standard Library and the Vaults of Parnassus
A default Python install includes many modules (blocks of code) that enable you to inter act with the operating system, open and manipulate files, parse command-line options, perform data hashing and encryption, and much more. This is one of the reasons most commonly cited when people are asked why they like Python so much—it comes stocked to the gills with functionality you can take advantage of immediately. In fact, the number of modules included in the Standard Library is so high that entire books have been written about them — try
For unofficial scripts and add-ons for Python, the recommended starting place is called the Vaults of Parnassus: http://py.vaults.ca/. There you can find about 20,000 public scripts and code examples for everything from mathematics to games.
Reference
> http://www.python.org/ — The Python website is packed with information and updated regularly. This should be your first stop, if only to read the latest Python news.
> http://www.zope.org/ — The home page of the Zope Content Management System (CMS), it's one of the most popular CMSes around and, more importantly, written entirely in Python.
> http://www.jython.org/ — Python also has an excellent Java-based interpreter to allow it to run on other platforms. If you prefer Microsoft's .NET, try http://www.codeplex.com/Wiki/View.aspx? ProjectName=IronPython.
> http://www.pythonline.com/ — Guido van Rossum borrowed the name for his language from Monty Python's Flying Circus, and as a result, many Python code examples use oblique Monty Python references. A visit to the official Monty Python site to hone your Python knowledge is highly recommended!
> http://www.python.org/moin/PythonBooks — There are few truly great books about Python; however, you can find a list of what's on offer at this site. If you are desperate to pick up a book immediately, you could do much worse than to choose
CHAPTER 27
Writing PHP Scripts
This chapter introduces you to the world of PHP programming, from the point of view of using it as a web scripting language and as a command-line tool. PHP originally stood for
Part of the success of PHP has been its powerful integration with databases — its earliest uses nearly always took advantage of a database back end. In PHP 5, however, two big new data storage mechanisms were introduced: SQLite, which is a powerful and local database system, and SimpleXML, which is an API designed to make XML parsing and querying easy. As you will see over time, the PHP developers did a great job because both SQLite and SimpleXML are easy to learn and use.
PHP's installation packages are under the Web Server category in Add/Remove Applications. The basic package is just called php
, but you might also want to add extensions such as php_ldap
, php_mysql
, or php_pgsql
. Choose only the extensions you plan to use; otherwise, you will waste system resources.
Introduction to PHP
In terms of the way it looks, PHP is a cross between Java and Perl, having taken the best aspects of both and merged them successfully into one language. The Java parts include a powerful object-orientation system, the capability to throw program exceptions, and the general style of writing that both languages borrowed from C. Borrowed from Perl is the 'it should just work' mentality where ease of use is favored over strictness. As a result, you will find a lot of 'there is more than one way to do it' in PHP.
Entering and Exiting PHP Mode
Unlike PHP's predecessors, you embed your PHP code inside your HTML as opposed to the other way around. Before PHP, many websites had standard HTML pages for most of their content, linking to Perl CGI pages to do back-end processing when needed. With PHP, all your pages are capable of processing and containing HTML.
Each .php
file is processed by PHP that looks for code to execute. PHP considers all the text it finds to be HTML until it finds one of four things:
> <?php
> <?
> <script language='php'>
The first option is the preferred method of entering PHP mode because it is guaranteed to work.
After you are in PHP mode, you can exit it by using ?>
(for <?php
and <?
); %>
for <%
) or </script>
(for <script language='php'>
). This code example demonstrates entering and exiting PHP mode: