Object Oriented Programming With PHP5: A Review

PHP, Personal No Comments »

Object Oriented Programming with PHP5I was asked by Packt Publishing to do another review. After such a positive experience reviewing Blogger: Beyond the Basics I agreed to review Object Oriented Programming with PHP5.

I chose this book because PHP is a very popular language but is easily misused and because I was hoping to find some bits that I didn’t know about OOP PHP.

I found that this book was full of very useful information even as an experienced PHP programmer. I did find some of the wording awkward and there were a few mistakes in the code samples when it came the case of some words but this did not diminish the excellent value this book provides to the reader.

I would say this book is ideal for the beginner to average PHP programmer or someone looking to start or improve their understanding of OOP in PHP.

Book Highlights

I want to take some time and touch on what I consider to be the best parts of the book and give you a small glimpse into what this book has to offer.

Chapter 2 - OOP Overview

Chapter 2 discusses what makes up an object oriented language. Essentially, this is the most important chapter in the book because if it were the only chapter you would walk away with everything you needed to begin using the OOP style of PHP. This was done very well, with lots of code examples to make the concepts easy to understand. Everything beyond this chapter builds on these concepts.

Chapter 4 - Design Patterns

This is another important chapter. Design patterns are problems that regularly present themselves and the recommended solutions for those problems. Design patterns are just guidelines on how to best handle a given situation. I really enjoyed this chapter and it really adds some extra punch to the book.

Chapter 5 - Unit Testing

I was thrilled to see that this was included in the book. Unit testing is very popular and very criticized depending on who you talk to. As the lead developer in my company it is my job to ensure the quality of our software and unit testing is a useful tool for that even when I have to constantly insist that tests get written.

Chapter 7 - MySQLi, OOP Database Access

This was an informative chapter however it is funny to note that while the chapter is supposed to be highlighting the OOP usage of MySQLi the author uses procedural methods several times throughout the chapter. He uses mysqli_connect_errno() and mysqli_connect_error() instead of $mysqli->connect_errno() and $mysqli->connect_error().

Chapter 9 - Building With MVC

MVC is all the rage these days and no book on OOP and Design Patterns would be complete without looking at MVC. MVC is a pattern used in many popular frameworks like Rails, Django, and Symfony. MVC can be done in many different ways ranging from complex to basic implementations. Frameworks like Rails have builtin utilities that generate the repetitive and structured code for you as a base to get you started quickly. I tend to roll my own implementations and keep it simple and fast depending on the scope of the project.

Conclusion

Once again, I was impressed by this book and was glad that I had picked it up. It is great for beginners or those looking to improve with OOP. Give it a read sometime. You’ll be glad you did.

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.

Friday Roundup for May 16, 2008

News, PHP No Comments »

Here is what I found interesting this week.

The future of PHP
PHP is already popular, used in millions of domains (according to Netcraft), supported by most ISPs and used by household-name Web companies like Yahoo! The upcoming versions of PHP aim to add to this success by introducing new features that make PHP more usable in some cases and more secure in others. Are you ready for PHP V6? If you were upgrading tomorrow, would your scripts execute just fine or would you have work to do? This article focuses on the changes for PHP V6 — some of them back-ported to versions PHP V5.x — that could require some tweaks to your current scripts.

The How Lame Is Technorati Experiment
This will be kind of a rant. What’s the point of Technorati? Can anyone tell me? Does anyone even go there any longer except me? Is there a point to it? I used to get a decent amount of traffic from them on a couple of other blogs, but here at Affiliate Confession, if traffic from Technorati were considered water, I would be dead.

A new spin on the datepicker control
Just when you thought that datepickers had been played out, along comes Filament Group and puts a whole new spin on it. Working from Mark Grabanski’s jQuery UI DatePicker control, the team substantially enhanced the UI with a host of new features

Icahn’s Yahoo fight puts Microsoft in driver’s seat
Icahn wants Yahoo to reopen talks with Microsoft, saying the company’s board had acted “irrationally” when it rejected Microsoft’s $47.5 billion buyout offer. Microsoft walked away from the deal earlier this month when Yahoo rejected its final offer of $33 a share, holding out for at least $37 a share.

CBS in US$1.8B deal for online news, info website CNet Networks
CNet was an early player in the dot-com boom and survived the subsequent crash with a steady focus on technology news, reviews and entertainment. But its stock, which once traded as high as $79 during the bubble, has slumped over the last two years, leading to an investor rebellion that was gathering steam just as the CBS deal was announced.

The $11.50 per-share price CBS is paying represents a huge premium of 45 per cent over CNet’s stock price the day before and seemed likely to resolve a looming proxy battle with its biggest investor, the hedge fund Jana Partners LLC, which has pressed for action to raise CNet’s stock price. Jana declined to comment.

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.

Add Ebay auctions to your website in 5 minutes

PHP, Projects 5 Comments »

Update: I mentioned that I would cover including your Ebay affiliate link in the feed in a later article, however, had I paid closer attention I would have seen that the affiliate link can easily be included when you create the feed.

Under advanced search go down to “Affiliate Tracking Information” and click show. Choose Commision Junction as the provider and enter your PID. That’s it know the feed links will include your affiliate id and you can get commissions on traffic you refer.

Have you ever wanted to add Ebay auctions to your website? Well, I’ll show you in a few easy steps how to do just that.

Getting started

Before we get into the real meat of this post we need to put some things in place. You will need the class file, which is below, and the url to the RSS feed you want to use. With these two things in place you will be able to add your feed in just four lines of code.

The RSS feed

Whenever you do a search on Ebay you are provided with an rss feed for that search. You will see the small orange RSS image after the search results in the Tools section. Here is an example of a seach RSS feed.

The class file

Save this file as EbayRssFeed.class.php and place it in the same directory as the php file you want to include the feed on.

<?php
	class EbayRssFeed {
		private $url;
		private $feed;

		public function __construct($url) {
			$this->url = $url;
			$this->feed = simplexml_load_file($url);
		}

		private function getItems() {
			return $this->feed->channel->item;
		}

		public function writeFeed() {
			$items = $this->getItems();
			echo "\n\n<!--EBAY RSS FEED-->\n";
			for($i=0;$i<count($items);$i++) {
				echo "<div class=\"EbayRssFeed\">\n\t<div class=\"EbayRssTitle\">" .
					"<a href=\"" . $items[$i]->link . "\">" . $items[$i]->title . "</a></div>" .
					"\n\t<div class=\"EbayRssDescription\">" . $items[$i]->description . "</div>\n</div>\n\n";
			}
			echo "<!--/EBAY RSS FEED-->\n\n";
		}
	}
?>

Adding the feed

Now that we have that out of the way let’s add that RSS feed.

First we will create the class instance.

//include the class file
require_once("EbayRssFeed.class.php");

//the url to our RSS feed
$file = 'http://rss.api.ebay.com/ws/rssapi?FeedName=SearchResults&siteId=0&language=en-US&output=RSS20&catref=C5&sacqy=&sacur=0&sorefinesearch=1&from=R14&fbd=1&saobfmts=exsif&_trksid=m37&dfsp=2&sacqyop=ge&saslc=0&floc=1&sabfmts=0&saprclo=250&saprchi=&saaff=afdefault&sabdlo=2&ftrv=1&sabdhi=&ftrt=1&fcl=3&frpp=50&afcj=&nojspr=y&satitle=vacation&afmp=&sacat=29578&saslop=1&fss=0';

//create the instance
$ebay = new EbayRssFeed($file);

Now, that wasn’t too hard was it?

Place the following code in your php page where you would like the feed to show.

<?= $ebay->writeFeed() ?>

Finishing up

Your done! That really is all there is to it. I plan on covering this in a later post and integrating your Ebay affiliate id so you can earn some cash from the users that are referred by your feed.

You can check out a demo that I made. Enjoy!

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.

The state of functional programming in PHP

PHP No Comments »

SitePoint has published a great article exploring functional programming in PHP.

With the rise of Javascript, and languages like Python and Ruby, functional programming is becoming more mainstream. Even Java seems to be getting closures in the next version, so does this leave PHP lacking behind or is there an unrealised potential hidden within?
Dynamic dispatch

What exactly defines a functional programming language, is perhaps an open question, but one key element is functions as data. As it happens, PHP kind-of-supports this concept; The syntax permits you to use variables as function-names, making the following possible:

function add($a, $b) {
  return $a + $b;
}
$add = "add";
$add(2, 8); // return 10

Unlike languages with first class functions support, the variable $add isn’t a special type — It’s merely a string, which is evaluated in the context. It may just be a wrapped up eval, but superficially it works similar, once the function has been declared.

It is also possible to explicitly call a function reference with call_user_func. This is interesting, because it accepts different types of arguments, which makes it possible to call a method on an object. More on this in a moment.

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.

Ensure that JavaScript files or CSS files are refreshed for each new version

ASP.Net, JavaScript/Ajax, PHP, Ruby/Rails, XHTML/CSS 2 Comments »

I found a nice article explaining how to ensure that the newest version of your javascript or css is always loaded and not using old cached versions. The example shows how to accomplish this in ASP.Net but this technique can easily be applied to any server side code.

<script type=”text/JavaScript ” src=”FileName.js?v=<%=AssemblyVersionNumber()%>”>

The attribute does nothing other than trick the browser into thinking that the .js file must be retrieved from server for new version instead of cached .

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.

PHP Variables

PHP No Comments »

By iain hendry

Variables

A variable is an area of memory which is set aside to store information , this is assigned an identifier by the programmer . You can recognise variables in PHP because they are prefixed with the dollar ( ) sign . To assign a variable you use the assignment operator ( = ) . Here is an example of this.

$name = "shedboy";
$intDaysInWeek = 7;

In the first example the variable identifier in this example is name and the string value “iain hendry” has been assigned to it . In the second example the variable identifier is intDaysInWeek and the number 7 is assigned to it . Note that in the number example we do not surround the variable with quotes in this way PHP treats this as a numeric value but if we had put quotes round it then PHP would have treated it as a string.

Variable Naming

There are some guidelines to follow for naming variables in PHP and these are as follows .
1. All variable names must begin with a letter or underscore character .
2 . The name of the variable can be made up of numbers , letters , underscores but you cant use charcters like , - , & , £ , , etc
One important thing to note if you are coming from another programming language there is no size limit for variables .

Case Sensitivity

One thing that causes many hours of hair pulling and anguish is case sensitivity , PHP is case sensitive (some languages are not) . Here is an example of what I mean.

$myname = "shedboy";
echo $Myname";

Now we all know what we want to do , declare a variable , assign it the value of “shedboy” and then print this on the screen but in this example we have mis-spelt the variable name , when run the following error is displayed on the screen.

Warning: Undefined variable: Myname in D:testsample.php on line 3

Example

Using your favourite HTML editor enter the following

$url = "http://www.geekdaily.net";
$rank = 1;
echo "Our favourite site is ".$url;
echo "“;
echo “It is numbered “.$rank;

If you have enterd this correctly you should get the following displayed

Our favourite site is http://www.geekdaily.net
It is number 1

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.
WP Theme & Icons by N.Design Studio
Entries RSS Login