A soft introduction to object oriented programming.

KBleivik
Site Admin
Posts: 88
Joined: Tue Jan 31, 2006 3:10 pm
Location: Moss Norway
Contact:

A soft introduction to object oriented programming.

Postby KBleivik » Wed Feb 01, 2006 5:45 pm

1. Introduction.
By using the JavaScript Create Object, you can get a soft introduction to object oriented programming (OOP).

The resources at W3Schools, where you can learn the basics of nearly all web design are excellent. It has an inbuilt editor where you can modify example code and see the effect immediately.

2. Start.
http://www.w3schools.com/js/js_objects.asp

Read that section and click on the examples (takes 5 to 10 minutes).

- Create a direct instance of an object.

- Create a template for an object.

In the last example modify the code in the left window to: (Modifications in bold)

<html>
<body>

<script type="text/javascript">

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname
this.lastname=lastname
this.age=age
this.eyecolor=eyecolor

this.newfirstname=newfirstname
}

function newfirstname(new_firstname)
{
this.firstname=new_firstname
}

myFather=new person("John","Doe",50,"blue")

myFather.newfirstname("James")

document.write(myFather.firstname + " is " + myFather.age + " years old.")

</script>

</body>
</html>

and click the button "Edit the text and click me".

Note the difference.

3. Exercise.
Write a function that changes the persons age, set the new age to 60 and submit the change.

Now you have a vague idea of OOP.

4. Next step.
Download free the first three chapters of the book "The PHP Antohology: Object Oriented PHP Solutions , Volume 1." by Harry Fuecks from SitePoint.
http://www.sitepoint.com/books/phpant1/

Chapter 2. Object Oriented PHP explains how you may make your own page object. Then you can add the properties and layout you want to that object and make your own page templates, eg. a table with three tables for header, body and footer with different properties etc.

Once you understand OOP, you will love it.

Also start your first step on Extreme Programming XP, by writing the tests of your code before anything else:

Exapmle: Again we use the resources at W3schools:
http://www.w3schools.com/js/js_onerror.asp

Use the code in "The onerror event" example to write a test to test for JS errors on the page.

Copy this code:
<script type="text/javascript">
onerror=handleErr
var txt=""

function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + msg + "\n"
txt+="URL: " + url + "\n"
txt+="Line: " + l + "\n\n"
txt+="Click OK to continue.\n\n"
alert(txt)
return true
}

function message()
{
alert("JS code is OK!")
}
</script>

in the head section and this code in the body section before your own code:

Write this code in the body section:
<input type="button" value="View message" onclick="message()" />

You may improve the test by using JS Try...Catch and Throw statements.

5. An introduction to OOP PHP and MySQL.
From SitePoint you can also download free the first four chapters of the book: "Build Your Own Database Driven Website Using PHP and MySQL" by Kevin Yank.
http://www.sitepoint.com/books/phpmysql1/

Learning this technique you can buld you own content management system, and refactoring and you get full control over refactoring and updating.

Note: The great difference between PHP and JavaScript (JS) is that JS code is interpreted by the browser (on the client's Pc) and PHP is interpreted by the webserver. Therefore, the advantage of JS is more flexible dynamic bahavior (action when the mouse is moved) while the disadvantage is that JS is browser dependent.

Some useful links:
http://www.w3schools.com/
http://www.w3schools.com/js/js_obj_htmldom.asp
http://www.w3schools.com/jsref/jsref_events.asp
http://msdn.microsoft.com/library/defau ... ptinga.asp
http://www.javascripter.net/index.html
http://msdn.microsoft.com/library/defau ... odstoc.asp
http://www.javascripter.net/faq/javascr4.htm
http://msdn.microsoft.com/library/defau ... curity.asp
http://www.hotscripts.com/
http://www.24fun.com/

http://www.php.net/

http://www.mysql.com/

http://www.sitepoint.com/forums/

Code is Queen, refactoring is King.

Recommended book: "The Design and Evolution of C++" by Bjarne Strostrup. ISBN 0-201-54330-3

That book takes to a new level in OOP.

6. Additional resources at OopSchool.com.
You find additional resources at:
http://www.oopschool.com
Kjell Gunnar Bleivik
Make it simple, as simple as possible but no simpler: | DigitalPunkt.no |

Return to “Generally”

Who is online

Users browsing this forum: No registered users and 1 guest