-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
The array_change_key_case function allows you to manipulate the textual data in an array by transforming it into uppercase or lowercase. Syntax: array_change_key_case(array, CASE_LOWER or CASE_UPPER) The array is the name of the array whose values you wish to convert to upper or lowercase. The parameter CASE_LOWER or CASE_UPPER is used to determine whether or not you are going to convert the values in the array to upper or lower case. Programming Samples and Usage: lt;?php $test = array ( quot;a quot; = gt; quot;Superman quot;, quot;b quot; = gt; quot;Batman quot;, quot;c quot; = gt; qu...
-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
This function takes two existing arrays and combines them into one array. Note that the first array must contain the key values that you want, while the second array must contain the values. Syntax: array_combine(name of the first array(keys), name of the second array(values)) Programming Samples and Usage: lt;?php $test = array ( quot;a quot;, quot;b quot;, quot;c quot;); $test2 = array ( quot;Superman quot;, quot;Batman quot;, quot;Sandman quot;); print_r(array_combine($test,$test2)); ? gt; The above code takes the values in the array $test (representing the keys) and combines them wi...
-
-
0:20
»
Open Source Web Development Tutorials - RSS Feeds
This function takes an existing array and breaks it up into two or more arrays. Syntax: array(array, array size, true/false) The array signifies the array you wish to use. The size is the number of elements that the new arrays will have. The true/false value is technically known as the preserve_key. It is optional and basically dictates whether or not the keys from the originating array are quot;preserved quot; or not. Programming Samples and Usage: lt;?php $test = array ( quot;a quot; = gt; quot;Superman quot;, quot;b quot; = gt; quot;Batman quot;, quot;c quot; = gt; quot;Sandman quot...
-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
As an important part of PHP 5.3's new features, closures have quickly made a mark on the language's field. This is due to their versatile nature, which allows programmers to utilize them in a wide variety of use cases and situations. They not only yield great results when processing array elements in all sorts of clever ways; it's fairly easy to get excellent results with them in the object-oriented programming arena as well. The best way to show you how using anonymous functions can help you to develop more efficient OO applications is with some functional, hands-on examples. With this idea i...
-
-
10:06
»
Open Source Web Development Tutorials - RSS Feeds
Unquestionably, the release of PHP 5.3.x, along with the imminent arrival of PHP 5.4 (at least, at the time of this writing), clearly show the level of maturity that the language has reached in the last few years. The inclusion of support for native namespaces, Late Static Binding, a largely improved SPL and of course the long-awaited traits, are all part of the wealth of niceties that PHP offers to picky developers. Although most of these features have found their own niche over time (with the sole exception of traits, for obvious reasons), there's one more that, because of its versatile natu...
-
-
11:00
»
Open Source Web Development Tutorials - RSS Feeds
PHP's Program Execution Functions This section introduces several functions (in addition to the backticks execution operator) used to execute system-level programs via a PHP script. Although at first glance they all appear to be operationally identical, each offers its own syntactical nuances. exec() string exec (string command [, array output [, int return_var]]) The exec() function is best-suited for executing an operating system-level application (designated by command) intended to continue executing in the server background. Although the last line of output will be returned, chances ar...
-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
Let's be honest: how many times have you found yourself writing a custom static helper class, or putting your hands eagerly on one that came bundled with a framework, from the many available out there? If you're anything like me, the answer will be at least Â
a few (remember that the first step to healing is admitting you have a problem). Static helpers seem to be a great idea at first glance, as they're reusable components that don't require any kind of expensive instantiation for doing common tasks, such as determining base URLs and paths or validating incoming data. But the sad and unavoid...
-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
Constructing purely static helper classes can hurt you, especially when you're trying to build applications that adhere to the principles of good object-oriented programming. As I explained extensively in the first part of this tutorial, static helpers are inflexible structures that can only be extended via Inheritance, and that don't exploit the benefits of Polymorphism or dependency injection. Naturally, the most educational way to learn why static helpers (or any other kind of static class, of course) should be replaced with instantiable classes is through some concrete, easy-to-foll...
-
-
10:30
»
Open Source Web Development Tutorials - RSS Feeds
System-Level Program Execution Truly lazy programmers know how to make the most of their entire server environment when developing applications, which includes exploiting the functionality of the operating system, file system, installed program base, and programming languages whenever necessary. In this section, you'll learn how PHP can interact with the operating system to call both OS-level programs and third-party installed applications. Done properly, it adds a whole new level of functionality to your PHP programming repertoire. Done poorly, it can be catastrophic not only to your applica...
-
-
8:30
»
Open Source Web Development Tutorials - RSS Feeds
Writing to a File This section highlights several of the functions used to output data to a file. fwrite() int fwrite (resource handle, string string [, int length]) The fwrite() function outputs the contents of string to the resource pointed to by handle. If the optional length parameter is provided, fwrite() will stop writing when length characters have been written. Otherwise, writing will stop when the end of the string is found. Consider this example: lt;?php $subscriberInfo = quot;Jason Gilmore|wj@example.com quot;; $fh = fopen( quot;/home/www/data/subscribers.txt quot;, quot;at ...
-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
Reading from a File PHP offers numerous methods for reading data from a file, ranging from reading in just one character at a time to reading in the entire file with a single operation. Many of the most useful functions are introduced in this section. file() array file (string filename [int use_include_path [, resource context]]) The immensely useful file() function is capable of reading a file into an array, separating each element by the newline character, with the newline still attached to the end of each element. Although simplistic, the importance of this function can't be understated...
-
-
9:00
»
Open Source Web Development Tutorials - RSS Feeds
File Ownership and Permissions These days, security is paramount to any server installation, large or small. Most modern operating systems have embraced the concept of the separation of file rights via a user/group ownership paradigm, which, when properly configured, offers a wonderfully convenient and powerful means for securing data. In this section, you'll learn how to use PHP's built-in functionality to review and manage these permissions. Note that because PHP scripts typically execute under the guise of the server daemon process owner, some of these functions will fail unless highly in...
-
-
12:00
»
Open Source Web Development Tutorials - RSS Feeds
As one of the building blocks of the Standard PHP Library (SPL), the ArrayObject class is a traversable, countable structure, which allows you to treat objects as arrays. This functionality seems irrelevant at first sight, but the class may be a real time-saver in certain situations. That's especially true when you need to create custom implementations (for instance, a dynamic registry or a service locator) without having to implement from scratch the ArrayAccess, IteratorAggregate and Countable SPL interfaces. The best way to get things rolling with ArrayObject is by example. In the first pa...
-
11:30
»
Open Source Web Development Tutorials - RSS Feeds
Added to PHP5 and improved in successive releases, the Standard PHP Library (SPL) is one of those things that, once you've used it, makes you wonder how you could live so long (and so painfully) without it. The library provides, right out the box, a set of native interfaces and classes which allow you to perform different tasks, without having to enter quot;userland. quot; While the SPL's core functionality is unquestionably hard to beat, sometimes it's a little overwhelming; it includes so many classes, and some of them have been assigned some strange names (especially the native iterators)....
-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
Retrieving a Directory Size PHP doesn't currently offer a standard function for retrieving the total size of a directory, a task more often required than retrieving total disk space (see disk_total_space()). And although you could make a system-level call to du using exec() or system() (both of which are introduced later in this chapter), such functions are often disabled for security reasons. The alternative solution is to write a custom PHP function that is capable of carrying out this task. A recursive function seems particularly well-suited for this task. One possible variation is offered...
-
-
11:00
»
Open Source Web Development Tutorials - RSS Feeds
File Types and Links Numerous functions are available for learning various details about files and links (or file pointers) found on a file system. Those functions are introduced in this section. filetype() string filetype (string filename) The filetype() function determines and returns the file type of filename. Eight values are possible: block: A block device such as a floppy disk drive or CD-ROM. char: A character device, which is responsible for a nonbuffered exchange of data between the operating system and a device such as a terminal or printer. dir: A directory. fifo: A ...
-
-
7:30
»
Open Source Web Development Tutorials - RSS Feeds
It's quite rare to write an application that is entirely self-sufficient--that is, a program that does not rely on at least some level of interaction with external resources, such as the underlying file and operating system, and even other programming languages. The reason for this is simple: As languages, file systems, and operating systems have matured, the opportunities for creating much more efficient, scalable, and timely applications have increased greatly as a result of the developer's ability to integrate the tried-and-true features of each component into a singular product. Of course,...
-
-
23:14
»
Open Source Web Development Tutorials - RSS Feeds
While proxy objects can be utilized in a variety of situations and environments, they yield excellent results when lazy-loading data from an application's persistence layer (usually this layer is seated upon a RDBMS, but it could be a web service or anything else). With the progressive adoption in the PHP field of Domain-Driven Design methodologies, the use of proxies for loading and reconstituting collections of domain objects on request from the underlying storage can help in the implementation of more efficient persistence strategies. This is true despite the need in some cases to create a ...
-
-
11:00
»
Open Source Web Development Tutorials - RSS Feeds
Although they still haven't gained the huge level of popularity of other design patterns (at least in the PHP field), proxies are a powerful paradigm that allows users to perform some clever tasks in OOP. These include unit testing classes, introspecting other objects, and even implementing more efficient data persistence strategies (pretty similar to what Doctrine 2.x does behind the scenes with entities). Of course, talking about the virtues that proxy objects offer without backing up the words with code samples is pointless. Therefore, in the introductory part of this tutorial, I started d...
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
There's no doubt that the most challenging facet in the development of an MVC-based web application, even the simplest one, is the implementation of the model(s). While controllers and views are inherently relevant pieces within the pattern's schema, they can be created to display a more relaxed behavior (at least to some extent), and even work decently upon a poorly-designed infrastructure, be it a full framework or a set of independent libraries. On the other hand, the model is a pristine and carefully-crafted creature, responsible for the application's state. Since it plays such a crucial r...
-
-
14:41
»
Open Source Web Development Tutorials - RSS Feeds
And now that you know what to expect, it's tiem to recap the topics discussed in the last installment. In that tutorial, I constructed a pair of dependency injection containers, along with a static helper. These allowed us to shield from client code the creation of the object graph corresponding to the user service. With the implementation of these additional elements, the scene is finally set to put the service into action and see if it is actually as functional as it seems. In this last chapter I'll be creating a concrete example that will show you how to use the service to retrieve, save a...
-
10:47
»
Open Source Web Development Tutorials - RSS Feeds
If you need to develop flexible, highly-extendable PHP applications that can be interfaced easily with different client layers, you will want to consider a service layer. As a fundamental pillar of Domain Driven Design (DDD), a service is an enterprise-level pattern that will let you encapsulate application logic behind a single interface -- which can be consumed by distinct clients (yes, including your carefully-crafted action controllers!) while keeping code duplication to a minimum. There's little sense to highlighting the benefits of a service layer without providing proof. To partially ad...
-
10:47
»
Open Source Web Development Tutorials - RSS Feeds
While the term may make you nervous, the truth is a service is nothing but an abstraction layer which permits you to interface application logic with different client layers. These client layers can range from several front-ends, to the so-called action controllers that can be found in many MVC frameworks available today. Put in a simpler way, a service is an enterprise-level pattern, and as such, it's both language and platform agnostic. This means that it can be implemented in most programming languages, including PHP, even though in general it's not considered by many to be a true enterpri...
-
10:47
»
Open Source Web Development Tutorials - RSS Feeds
Superbly covered in Martin Fowler's book Patterns of Enterprise Application Architecture, a service layer (http://martinfowler.com/eaaCatalog/serviceLayer.html) is an enterprise-level pattern that can be used for encapsulating application logic behind a single interface. This interface can be used by different clients. As with a few other patterns available, a service is particularly useful in the development of applications that use the domain-driven design paradigm (DDD), where the domain model has its own constraints and rules, and lives completely isolated from the persistence layer (the s...
-
10:47
»
Open Source Web Development Tutorials - RSS Feeds
If you ever need to implement a pluggable structure that allow your PHP applications to interact easily with different client layers, such as several front-ends or multiple external APIs, then you might want to consider a service layer (http://martinfowler.com/eaaCatalog/serviceLayer.html). A service layer is an enterprise-level pattern that will let you achieve this kind of interaction by encapsulating application logic behind a single interface (AKA a boundary), which can be directly consumed by distinct clients. Of course, highlighting the benefits that a service offers without adding some ...
-
10:47
»
Open Source Web Development Tutorials - RSS Feeds
Although its name may sound intimidating, a service is nothing but an additional abstraction layer (usually placed on top of the mapping one). It permits you to encapsulate application logic behind a single entry-point interface that can be consumed by several different clients, without having to deal with frustrating code duplication issues. As with any other design pattern, the creation of a service isn't tied to a particular programming language or platform. This means that it's perfectly possible to implement it in a fairly straightforward fashion with PHP and enjoy the benefits that it pr...
-
-
1:35
»
Open Source Web Development Tutorials - RSS Feeds
While an MVC stack can be implemented in several different ways (quite possibly there exists one per developer), you know that a minimalist approach goes in general with the following application flow: once a request has been triggered within the user interface, the front controller does some routing and dispatches the request to the appropriate action controller, along with the associated argument(s). In turn, the action controller grabs one or more models and uses them to perform CRUD operations in the persistence layer (usually a RDBMS, but it could be a web service or anything else). Fina...
-
-
9:32
»
Open Source Web Development Tutorials - RSS Feeds
If the name doesn't ring any bells, MongoDB (http://www.mongodb.org) is a highly-scalable, document-oriented database written in C++, which permits you to store data by utilizing a JSON-like representation. This means that you can perform CRUD operations (among others) without having to deal with the pitfalls and mismatch issues of an RDBMS, especially when it comes to handling objects, whose state must be persisted in tables. If you already read the two previous installments of this tutorial, at this stage you should have a clear idea of how to put MongoDB to work for you, as I explained how ...
-
-
22:11
»
Open Source Web Development Tutorials - RSS Feeds
If you think that the group of RDBMS' out there fall short when it comes to persisting the host of objects that comprise your applications, maybe it is time to get rid of the constraints imposed by relational databases and take a look at MongoDB (http://www.mongodb.org), a schema-less, document-oriented database written in C++, which lets you store your carefully-crafted objects by using a JSON-like representation known as BSON (http://bsonspec.org). Although it's fair to admit that object-oriented databases don't have much of a presence in the terrain of web development, which is in most cas...
-
-
10:23
»
Open Source Web Development Tutorials - RSS Feeds
Without a doubt, RDBMS' (Relational Database Management Systems) rule the world of data persistence. Backed by a huge (and growing) number of manufacturers, which cover a plethoric variety of platforms and environments, relational databases are - in most cases - the default mechanism for storing information in a relatively straightforward manner. However, not all is wonderful in the terrain of RDBMS, since the use of tables isn't always the best way to persist objects (either real or virtual ones). In many use cases (like persisting blog posts), the storage process suffers from an issue common...
-
-
7:30
»
Open Source Web Development Tutorials - RSS Feeds
Recent advancements in PHP offer the developer a variety of tools to improve the security of login systems. The login script we will be developing consists of the following very important security features: 1.) A Hashed password (SHA-256+ Strong Salt) stored in a MySQL database. 2.) Anti-brute force protection using captcha challenges and denied access for more than X failed attempts relating to a specific IP address. 3.) User registration by providing username and password - protected with a captcha to prevent automated sign-ups. 4.) Protection of user-sessions against session hijacking and...
-
-
9:00
»
Open Source Web Development Tutorials - RSS Feeds
Suppose a test is done comparing the speed between two possible approaches in coding PHP that yields the following average results: Test 1 average execution time = 630 Microseconds Test 2 average execution time =602 Microseconds You cannot simply conclude and recommend that the Test 2 approach is faster and better simply because it has a lower average or mean (which means faster). Numerically, 602 microseconds lt; 630 microseconds and the difference between the results is 630 - 602 = 28 microseconds. The main question, however, that is of so much importance to PHP developers/coders/programmer...