encodings

Text has to be stored as zero’s and one’s (naturally…), and there are multiple standards to convert binary to text.
Lets have a quick overview:

ASCII
ASCII is both a standard and an encoding. The encoding called ASCII used to have only 7 bits. The standard called ASCII is widely used in other encoding as well, which basically means in all of those encoding certain letters have the same binary representation (with more leading 0s for 8 bits or even bigger encodings). For example the letter 1101110 in ASCII could be represented as 01101110 in an 8 bits encoding.

ISO 8859-1 or latin1
This is an encoding widely used for european languages. It is ASCII compatible, and uses 8 bits. Unfortunately, 8 bits are not enough to include arabic letters or even chinese one, which means there are a lot of similar ASCII compatible 8 bit encoding.

UTF-8, UTF-16, UTF-32,..
UTF is one of the solutions to the ISO encodings mess. It is also ASCII compatible, but does not have a fixed size. if you use UTF-8, every letter will have at least 8 bits, but less common ones as äöü can have 16 bits, 32 bits or even more. Now all characters there are can be used with the same encoding!

With this in mind, you can now understand what went wrong when looking at misinterpreted text:

latin1 vs utf8

left: correct; middle: UTF-8 interpreted as latin1; right: latin1 interpreted as UTF-8

php autoloading

In php, before you can use a class or method inside a file, you have to include the file first.

As it is cumbersome and error-prone to do this manually, you should use autoloading. You have to meet two requirements for this to work best:

  • Use a valid Objective Oriented approach: for example don’t globally define methods, but group similar methods in classes (example: define a public static function convertToInches in the class UnitConverter instead of just defining it globally)
  • Use namespaces: Use namespaces corresponding to the folder structure of your project. Learn about this at PSR-4

The autoloading itself is quite simple and saves you a lot of time. Place the code below at the entry point of your application.

/*** class loader ***/
function load_my_classes($class) {
    // project-specific namespace prefix
    $prefix = 'Vendor\\Project\\';

    // does the class use the namespace prefix?
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) === 0) {
		// get the relative class name
                $relative_class = substr($class, $len);
    
		// replace namespace separators with directory separators in the relative class name
		// append .php
		$file = __DIR__ . "/" . str_replace('\\', '/', $relative_class) . '.php';

		// if the file exists, require it
		if (file_exists($file)) {
			require $file;
		} else {    
			throw new Exception("Unable to load class: " . $class . " @ path: " . $file);
		}
	} else {
		//as the class uses an unknown namespace prefix, we ignore this one. An other autoloader can take care of this.
	}
});

/*** register the class loader ***/
spl_autoload_register('spl_autoload_register');

Now, whenever php has to use a class it does not know yet, it calls all methods registered by spl_autoload_register (in our case this is load_my_classes), until the class it included.
If it has called all registered methods, but still the class is not found, your application terminates (obviously).

If you’ve followed the naming conventions as specified in PSR-4, you can even take this a step further with composer. Besides other very useful things, composer does automatically generate the autoloading code. It’s worth giving it a try.

async await

async-await is a very important concept from C#. It allows you to execute code in another thread than from the calling one; this results in a responsive UI even while  long background tasks are running.

Some examples:

/* ## Example 1 ## */
public async Task<bool> Execute()
{
    //long running CPU bound operation
    await Task.Run(() => {
    for (int i = 0; i != 10000000; ++i)
        ;
    });

    //long running I/O Operation
    using (var client = new HttpClient())
    {
        await client.GetStringAsync(url);
    }
    return true;
}

//call this method
var response = await Execute();

/* ## Example 2 ## */
//remove the async-await if not needed, and return directly the Task.
public Task<string> Execute2()
{
    //long running I/O Operation
    using (var client = new HttpClient())
    {
        return client.GetStringAsync(url);
    }
}

//call this method
var response = await Execute2();

/* ## Example 3 ## */
//add the async keyword to event handlers to use them asynchronusly
public async void OnTapped()
{
    //long running I/O Operation
    using (var client = new HttpClient())
    {
        await client.GetStringAsync(url);
    }
}

From my own experiences, the simpler the expressions, the better. If it looks wrong or overcomplicated to you, it probably is. I recommend to take a look at Stephen Cleary’s Blog (specifically at async-await) for extensive information and tutorials about async-await.