Author: karlbunyan

PHP 5: Class hinting

PHP 5: Class hinting

Class hinting is where the class of object to be passed into a method is specified in the function call. e.g. abstract class User { protected $logState; public function User() { $this->logState = new LogState() } public function setLogstate(LogState $logState) { $this->logState = $logState; } } This code will throw a runtime error if the …

+ Read More

PHP 5: Interfaces

PHP 5: Interfaces

Interfaces are a way of adding the definition of extra methods onto a class to force it to conform to a particular pattern. An interface consists entirely of methods with empty bodies i.e. abstract methods: interface Saveable() { public function save(); } Classes implement interfaces as a way of guaranteeing that they will allow a …

+ Read More

PHP 5: Static classes

PHP 5: Static classes

Static classes are used for utility scripts that (in the past) would have been in common functions or held global variables. The advantage of using a static class is more to increase code readability (and ease of debugging) than for any other reason. A static class may have properties and methods but there will always …

+ Read More

PHP 5: Abstract classes

PHP 5: Abstract classes

Abstract classes are used to define operations and parameters but where you do not want a class instantiated directly. An abstract class must, therefore, have subclasses. In our example, we may not allow a type of general “User” but insist that a specific type is created. We can do this by defining the class as …

+ Read More

PHP 5: Public, private, protected

PHP 5: Public, private, protected

PHP 5 allows you to declare properties and methods as public, private or protected. These are defined as: Public: anyone either inside the class or outside can access them Private: only the specified class can access them. Even subclasses will be denied access. Protected: only the specified class and subclasses can access them It is …

+ Read More

PHP 5: The constructor method

PHP 5: The constructor method

The introduction of a method called __construct() means that function User() no longer need be the default method name for instantiating an instance of the User class. This is especially useful in the case where you are using subclasses, or if the name of the class changes during development. E.g. In the example above, the …

+ Read More

PHP 5: Subclasses

PHP 5: Subclasses

Subclassing allows flexibility in code. E.g. there may be many types of User to a site. We can define subclasses of User such as AdminUser, RegisteredUser, UnregisteredUser which may share some common methods and properties but may have unique abilities. The syntax for this is simple: class User { //constructor function for User function User() …

+ Read More

PHP 5: By reference or by value

PHP 5: By reference or by value

PHP 4 passed variables by value by default. This was not very pleasant to work with. The way around this was to pepper your code with ampersands (to tell PHP to use references instead) but forgetting to include one was difficult to debug. By default, PHP 5 passes all objects by reference. Therefore calling: $user->getLogin($_SESSION); …

+ Read More

PHP 5: Introduction to classes

PHP 5: Introduction to classes

Classes are used as an abstraction of a real world situation. E.g class User { } may be used to contain the code that will represent a user of an application. Objects have properties and methods. Properties may be thought of as adjectives (e.g. coat->blue) and methods as verbs (e.g. coat->putOn()). e.g. to create a …

+ Read More

Toyota demonstrates robot personal vehicles

Toyota demonstrates robot personal vehicles

I found the pictures of Toyota’s robot cars/personal vehicles very interesting. (‘Those whacky Japanese do it again’ was going to be my alternative headline, but that doesn’t give a great deal away about the content.) Okay, they may only travel at less than 1mph and look like a 1960’s sci-fi view of the future, but …

+ Read More