Final and auto load a class

To include a class within many languages you have to define it at the top and then off you go with programming with using that class. Well within PHP you can use the __autoload function that is called when you try to create a new instance of a class.

function __autoload($class_name) 
{
	include "classes/".$class_name.".php";
}

this __autoload function above takes a parameter as the class name to load and then within the code it will try and load that class with how ever you want to, in this case I have placed the classes within the directory called classes and under there classname.php filename.

So if you try to load the class

$newclass = new basic();

it will look within the classes directory for the file basic.php for the autoload function above, you can alter it to load from where ever and how ever you want to.

The next part of this post is about the keyword ‘final’, the final keyword means that it is final so if you try and extend that class and extend the function that has been declared as final this will cause php interpretor to fail due to syntax error with something similar to

Fatal error: Cannot override final method class_name::function_name()

For an example here is two classes one is called basic and the other is called extendbasic, where the basic class has the final function within it

class basic
{
	public function sayHello()
	{
		echo "<br/>".get_class($this) . " : Hello";
	}
 
	final public function sayGoodbye()
	{
		echo "<br/>".get_class($this) . " : Goodbye";
	}
}

and here is the extendclass

class extendbasic extends basic
{
	public function sayHello()
	{
		echo "<br/>".get_class($this). " : the newer hello!!";	
		parent::sayHello();
	}
        // this will cause the php engine to fail!!!!!. because it is trying to over load the function sayGoogbye which has been declared as final
/*	public function sayGoodbye()
	{
		echo "I should not get here!!";
	}*/
}

Here is some code to call them both which they have been placed within a directory called ‘classes’

function __autoload($class_name) 
{
	include "classes/".$class_name.".php";
}
 
$basicClass = new basic();
$basicClass->sayHello();
$basicClass->sayGoodbye();
$basicClass = new extendbasic();
$basicClass->sayHello();
$basicClass->sayGoodbye();

and the output would be

basic : Hello
basic : Goodbye
extendbasic : the newer hello!!
extendbasic : Hello
extendbasic : Goodbye

I am calling the parent function from within the extendbasic class sayHello function.

Node – install and test

One of my friends asked me what is Node and also how do you install and test to make sure it is working.

Well, Node is in essence a javascript server side environment that allows you to write code that will allow the event model to handle requests very quickly since it does not handle the I/O (well almost of all of the functions within node do not!) so there is no-blocking of code.

Node has a very quick and capable http class that allows for the user to create a server that is capable of doing some very quick responses to multiple connections.

To install Node you just need to do this in ubuntu

sudo -s
apt-get install python-software-properties
add-apt-repository ppa:chris-lea/node.js
apt-get update
apt-get install nodejs
exit

the first line allows you to be the root and thus the next commands are as the user root.

Or if you are using windows just download the executable from here

To test the node to make sure it is working with a basic “Hello world!” example if you save this below as helloworld.js.

var http = require("http");
// create the http server function req'uest, res'ult
http.createServer(function (req, res) 
{
    // 200 = means http response code of found page, then a array of the content type
    res.writeHead(200, {'Content-Type' : 'text/plain'});
    res.end('Hello world!');  // write out the text
}).listen(8080,'127.0.0.1');
// listen on port 8080 on the ip address 127.0.0.1
console.log('We have started, check your browser on http://127.0.0.1:8080/');

I am using a higher number than 1024 for the port because below that you need to be a root user or administrator on windows to allow anything to connect to those port numbers.

So if you now run the program with the javascript file above as the second parameter

node helloworld.js

and then if you open your browser at 127.0.0.1:8080 then you will see

Hello world!

IIS redirector – application request routing

A great thing with apache webserver is that you can redirect HTTP requests to another place within the site or even hosting local port, well with IIS you can achieve the same with Application Request Routing.

It is a great add-on that really empowers the admin users to achieve allot more “fun” things with IIS.

Ckeditor – not allow posting of images from the local harddrive

Within the CKEditor you can also add on to the prototypes that allow more control of what is allowed on your website.

Sometimes you do not want to allow the user to copy and paste local files to the CKEditor so this is what I done to remove the file:// from within the img tag within the copy and paste with using the CKEditor htmlDataProcessor group of functions, in this case the ‘toHtml’ function which is called when the user pastes any content into the CKEditor window. All I am doing it using the regular expression to search for the <img src=”file .. and replace with a <mg tag> which when they double click on that it will open the CKEditor’s image browser (which you can include a upload part to it as well 🙂 )

CKEDITOR.htmlDataProcessor.prototype.toHtml = function( data, fixForBody )
  {
    return data.replace(/\<img src=\"file:([^\>])*\>/ig,"<img title=\"Please use the Media/Image uploader because can not show files on your harddrive\" alt=\"Please use the Media/Image uploader because can not show files on your harddrive\"/>");
}

It was very helpful for me.

Classes

Since Python is a object oriented programming language then you can use things like classes to define a object, a class is a bunch of methods that act on the data that is stored within the class itself. You can store data within the class, but unlike c++/java/c# etc, there is no private/public/protected, everything is public accessible.

In the example below, the class constructor is called (__init__ method) and the first parameter is the class object itself, which is why it is there and for many reasons why it is called self as a variable name.

 
#  self means the object itself.
 
class mysimpleclass :
        __myvar = 0
        def __init__(self,value = 0) :
                self.__myvar = value
 
        def printValue(self) :
                print self.__myvar
 
simple = mysimpleclass(5)
simple.printValue()

the printValue method just does that, it will print the value that is sorted within the class object and here is the output

5

if you did not put in the self in the example above for printing out the class object variable __myvar as below

class mysimpleclass ....
       ....
       def printValue() :
            print __myvar

the error would be

NameError: global name '_mysimpleclass__myvar' is not defined

because the interrupter is looking for a global variable and not a variable attached to that object.

Add two numbers

To carry on my normal process of notes on this website for different languages here is the how to add two numbers with a return value from a function.

To define a function you use the “def” syntax within the programming language and then the next bit of text before the parameters for the function itself ( in this case the parameters are the (number1, number2) ), is the function name and how to call the function.

The function below takes two parameters and returns back there result of the mathematical addition, the last part is how to call the function.

def addTwo(number1, number2) : 
	return number1 + number2
 
print addTwo(1,2)

and the output would be

3