subscribe: Posts | Comments

Programming PHP pages


The first thing we will do is create a PHP page to inform us of the
characteristics of the PHP interpreter that we installed on our web server. We
must create a PHP page with the following content:
<? phpinfo ();?>

This should be the content of a file called "info.php" and that it should be
somewhere accessible by the web server. Perhaps the best thing is that you save
in your web directory: "/ home / tu_login / public_html". For example I have in
the directory "/ home / acs / public_html / info.php." Once you have saved we
will try to open this page. To do this you browser pointing to the page "http://localhost/
~ tu_login / info.php". In my case this page is: http://localhost/ ~ acs /
info.php. "The result should get should be something similar to the following
Figure 1.

By asking this web page to the server, to see this extension calls the PHP interpreter and told to "run" the page and give you the results. The interpreter opens the page and executes the function call to "phpinfo ()" that as a result, gives us all the information about the PHP interpreter in HTML format. Is that easy?.

Vamos a page with something more complicated. And we’re going to object orientation. PHP has support for object oriented programming is, you can create classes for the construction of objects, etc with their builders. The objects that we support in PHP is not as powerful as C + +, but it is becoming more complete and in this new version of the PHP4 support has been improved.

If you do not know to program using objects in this course you will learn something but I recommend that you immediately get a good book or online tutorial. The objects are becoming harder to develop software. Either way this course we will use programming with objects in a very basic level.

In our next example we create an object that will allow us to access various databases. It is clear that we are going to leave without even deploy in large part, but everything will come. Here is the code of the page:
  <? php
  class Connection (
      dbug var = $ 1, $ to, $ type;

      gettype function () (global $ type; return $ this-> type;)

      / / Constructor of the connection object to the database
      function connection ($ typedb = "MySQL", $ database_name = "template1") (
          $ this-> connection_open ($ typedb, $ database_name);

      / / Open connection to database
      connection_open function ($ typedb, $ database_name) (
          $ this-> $ type = $ typedb;
          if ($ type == "PostgreSQL") $ this-> $ con = new postgresql_connection;
          elseif ($ type == "MySQL") $ this-> $ con = new mysql_connection;
          else (error_message ( "Open: Unkown database type"); exit;)
          $ this-> $ con-> connection_open ($ database_name);

  (mysql_connection class
      mysql_connection function () (
          echo "<h1> Class mysql_connection is not yet implemented. <h1>";
      connection_open function ($ database_name) (
          echo "<h1> Method connection_open unimplemented. <h1>";

  $ bd = new connection ( "MySQL", "template1");

If the reader is accustomed to programming with objects this code you will find very familiar. We have a main class "connection" that manages the connections to the database. When we want from our program to access a database, we create a Connection object by passing it as parameters to the constructor of connection, a method is called automatically when you create the object, the type of database you want to access the name and the specific database with which we work.

This object delegates to specific objects in each database, the database manager MySQL is "mysql_connection", opening a connection, so SQL requests. This technique of using objects to delegate the final work is widely used in other languages that have no inheritance mechanism whereby a class inherits all the state information and methods of a class that inherits. PHP3 in its early versions had no support for inheritance so I was forced to use this technique. At present, PHP and supports inheritance, but not multiple. And no, all the rules of public and private encapsulation are not implemented. We must be careful with anything that can give us many headaches: When you create an object of a class B that inherits from a class A, is called automatically by the constructor of class B but not to his father, Class A.

The PHP syntax is very similar to C or C + +. Perhaps the highlight occurs at the semantic level: the typing is very strict, some typical language script, and when we create a variable in it can get the kind of data we want. This is very flexible and convenient for the developer, although the errors made can be much more serious and difficult to correct. The ability of the interpreter to detect incompatibilities between variables is reduced greatly and strange things can happen in certain circumstances if we are not careful .

On the code in this particular example, we have two classes. The definition of a class is done with the keyword "class" followed by the name of the class. Bracketed develop a description of the class variables and methods. Within a class method (function) you can access other parts of the class via pointer "$ this". For example, to access the method "connection_open" to the class variable "$ type" do it with "$ this-> $ type."

If we keep this class in a file, eg "ejemploDB.php" when accessing this file via the web server, this call back to the PHP interpreter to see the file extension is "php". The interpreter examine the PHP code and execute the instruction:
  $ bd = new connection ( "MySQL", "template1");

This will create a new object of class "connection." When set up will automatically call the constructor of "connection function" which in turn calls the method "connection_open." This method creates a new object of class "mysql_connection" ($ con) which will automatically call its constructor " mysql_connection function "which sends the string HTML" <h1> Class mysql_connection is not yet implemented. <h1>. Since the method connection_open "of" connection "is called" connection_open "Object" $ in ", which is the class mysql_connection, which causes another string to print HTML" <h1> Method connection_open unimplemented. < h1>.. " After this invocation the program ends and sends the result to the web server, which forwards this data to the client. In Figure 2 we can see the result of such implementation.