Book Review: PHP 6 and MySQL 5 for Dynamic Web Sites



This book has actually been on my bookshelf for quite a while, I bought it a few months before the promised release of PHP 6 in 2008 (which never happend).

Unfortunately I didn't realise at the time that the author made a typo when he gave the book its title, since the book primarily focuses on functionality thats been around since PHP 4 - with some PHP 5 examples (e.g. mysqli extension).

PHP 6 is more like a side note in this book.

I can however imagine that it is quite difficult to write a book about a product that strictly speaking doesn't actually exist - so I will cut the author some slack. (apparently a lot, if not most of the suggested PHP 6 functionality are currently available within PHP 5.3)

Looking at the actual content, the table of contents reveals that this book is intended for beginners - so don't expect finding any nuclear bomb blueprints in this book (incidently).

I personally feel that the structure of this book is a bit illogical, for example in Chapter 3 on page 89 - 91 the author crudely explains how to create what he calls a sticky form (persisting selected/entered data in a form).

In order to get a less mediocre understanding of forms, the reader needs to advance to Chapter 8 on page 243 - 248 where they learn about magic quotes (which potentially messed up their sticky form back in Chapter 3) and SQL injection (which potentially messed up their database if they had one).

Another crucial part that relates to the sticky form only really gets discussed in Chapter 12 - form/data validation, html encoding (which potentially breaks their page layout) etc.

In my opinion it would have been more logical to explain all of these concepts together - since they are pretty much directly related to one another.

But then again thats my own preferences I guess, at least the book contains these concepts.

All in all I dont feel its a bad book (for a beginner) - definitely not what I expected or had in mind when I purchased it (I was expecting more of a PHP 6 primer) - don't judge a book by its title.

Additional Reading:

Have a look at the Errata if you're planning to buy this book, there is quite a number of little mistakes.

Also be sure to visit the author's (Larry Ullman) website www.dmcinsights.com





Post/View comments
 

C# Threading: Mutual exclusion (via Mutex Class)



Observe the following faulty snippet (don't use):

 
using System;
using System.Threading;
using System.IO;
 
class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < 3; i++)
        {
            Thread thread = new Thread(new ThreadStart(ThreadMain));
            thread.Name = String.Concat("Thread - ", i);
            thread.Start();
        }
    }
 
    static void ThreadMain()
    {
        // Simulate Some work
        Thread.Sleep(500);
 
        // Access a shared resource / critical section
        WriteToFile();
    }
 
    static void WriteToFile()
    {
        String ThreadName = Thread.CurrentThread.Name;
        Console.WriteLine("{0} using resource", ThreadName);
 
        try
        {
            using (StreamWriter sw = new StreamWriter("1.txt", true))
            {
                sw.WriteLine(ThreadName);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
 

What we've got here is three threads trying to write to a file, which fails horribly since we cant concurrently write to the same file.



We need some kind of synchronization mechanism to ensure that our threads access shared resources when appropriate.

In the .NET framework one of the solutions is to use the Mutex ("Mutual exclusion") class to aid us with this issue.

In the following snippet the issue gets resolved by adding a Mutex instance to it:

 
static Mutex mutex = new Mutex();
 
static void WriteToFile()
{
	mutex.WaitOne();
 
	String ThreadName = Thread.CurrentThread.Name;
	Console.WriteLine("{0} using resource", ThreadName);
 
	try
	{
		using (StreamWriter sw = new StreamWriter("1.txt", true))
		{
			sw.WriteLine(ThreadName);
		}
	}
	catch (Exception ex)
	{
		Console.WriteLine(ex.Message);
	}
 
	Console.WriteLine("{0} releasing resource", ThreadName);
 
	mutex.ReleaseMutex();
}
 

The first thread that reaches the critical section (writing of the file) takes ownership of the mutex - the other threads simply wait for the mutex to be released by the thread that owns it - each taking ownership once its released etc etc etc.

Basically its like making a queue behind a resource and only one thread is allowed to use the resource at a time.



We do however get a more lightweight class in the .NET framework that provides mutual-exclusion as well, namely the Monitor class (or lock statement).

What makes the Mutex class different (and more powerful) can be observed in the following snippet:

 
using System;
using System.Threading;
 
class Program
{
    static void Main(string[] args)
    {
        bool createdNew;
 
        using (Mutex mutex = new Mutex(true, "App", out createdNew))
        {
            if (!createdNew)
            {
                Console.WriteLine("Application already open");
                return;
            }
 
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}
 

Mutexes can be accessed system wide and across app domains & terminals, in the preceding snippet we prevent an user of opening multiple instances of our process.

There are two types of Mutexes, local(unnamed) - which is only available within its parent process (like the first snippet) and global(named) which is available system wide (like in the last snippet).

Additionally we can set the visibility of a global(named) mutex with regards to Terminal Services. If our named mutex starts with "Global\", it becomes visible to all terminal sessions, if it starts with "Local\" its only available to the terminal session that created it.

Additional reading:
C# Threading: Mutual exclusion (via Monitor Class)
http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx




Post/View comments
 
First 16 17 18 19 20 21 22 23 24 25 Last / 62 Pages (124 Entries)

Latest Posts

Be the best stalker you can be


2011-12-13 22:33:54

Syntactic sugar (C#): Enum


2011-08-04 16:50:18

Top 5 posts

Simple WYSIWYG Editor


Creating a WYSIWYG textbox for your website is actually quite simple.
2007-02-01 12:00:00

Moving items between listboxes in ASP.net/PHP example


Move items between two listboxes in ASP.net(C#, VB.NET) and PHP
2008-06-12 17:07:43

Cross Browser Issues: Firefox Word Wrapping


Firefox word wrapping issues
2008-06-09 09:51:21

Populate a TreeView Control C#


Populate a TreeView control in a windows application.
2009-08-27 16:01:03

C# YouTube : Google API


Post on how to integrate with YouTube using the Google Data API
2011-03-12 08:37:51