Entries Tagged 'PHP' ↓
December 16th, 2003 — PHP
OK, hoping someone else has a better memory than me 
I’m pretty sure a couple of months ago I ran across a free class to extract data from text files, that worked something like:
bc. $f = new ParserThing();
$f->startString(’
Price:’);
$f->endString(’;
‘);
$match = $f->getMatch();
Now, does anyone remember where this class can be found? From what I remember it should be simpler to use than constructing regular expressions to parse data out from 15 webpages.
October 15th, 2003 — PHP
I’m interested in hearing what other people think about PHP database abstraction layers/wrappers; and what others have chosen to use.
I started off using the one that came in PHPLib (and “sendcard”:http://www.sendcard.org still uses this) but quickly moved to “PEAR::DB”:http://pear.php.net. I chose it because it seemed to be becoming the standard and I liked the API.
Now, 2 years on there still doesn’t seem to be a standard. “AdoDB”:http://php.weblogs.com has some nice features but I don’t like the public interface (I can’t comment on the interior code as I’ve never looked at it) - the use of properties (? - forgotten the name of @$this->foo@) instead of methods never feels quite right to me.
Every time I start a new project I have another look around. Anyone got recommendations for something to use in the RSS Aggregator?
*PS:* if you’ve used PEAR’s “DB_DataObject”:http://pear.php.net/manual/en/package.database.db-dataobject.php or something similar (maybe in another langauge) let me know if you found it useful or not as I’m tempted to try it for the aggregator.
October 15th, 2003 — PHP
In my OPML parsing class I have the following method:
bc. function getBlog()
{
static $count = 0;
static $total;
if ($total == ”) {
$total = count($this->data);
}
if ($total == $count) {
return false;
}
$blog = &new Blog($this->data[$count]);
$count++;
return $blog;
}
and in the Blog class I have:
bc. function getEntry()
{
static $count = 0;
static $total;
if ($total == ”) {
$total = count($this->data);
}
if ($total == $count) {
return false;
}
$item = &new Item($this->data[$count]);
$count++;
return $item;
}
Pretty much repeated code. Which is going to appear in many other classes as well.
So how can I factor this out? Is there a design pattern that should be used here? Or is this a duplication of code I have to live with?
October 6th, 2003 — PHP
Hoping someone can help me with some sample code… I’m looking for the neatest way to write the class to power the following @while@ loop:
bc. $opml = new OPML_Parser;
$opml->load(’http://blo.gs/2650/mySubscriptions.opml’);
$opml->parse();
bc. while($blog = $opml->getBlog()) {
// Check if already entered in database. If so, update
if ($blog->exists()) {
$blog->update();
} else {
// If not, store it.
$blog->save();
}
}
How do I return an instance of a class for each blog? And keep track of them (so the while loop will exit correctly)? The only way I can think of is to put all the objects into an array, and then loop over it; but this doesn’t sound to be the correct way to do it.
September 25th, 2003 — General Programming, PHP
Well, “this article”:http://www.phpbuilder.com/columns/bosanac20030225.php3?page=2&print_mode=1 at PHPBuilder.com is the first one ever to make me think _”Hmm, maybe iterators could be useful”_.
Following on from “Harry’s comments”:http://peter.mapledesign.co.uk/weblog/archives/using_classes_in_php.html#comments I’ve started to think about the design of the system I’m going to use for my RSS aggregator more. Originally I thought what he’d written in the comments was over-engineered, over-designed, put-in-an-object-cause-its-trendy. But now I’m not so sure. I’ve gone back to reading Bertrand Meyer’s book “Object Oriented Software Construction”:http://www.amazon.com/exec/obidos/tg/detail/-/0136291554/peterswebpage04 (available cheaply in the *UK* from “Computer Manuals”:http://www.compman.co.uk/cgi-win/browse.exe?ref=278755) now to learn how to design OO systems - something I’ve been meaning to read since Easter, but never got the enthusiasm for (after all, isn’t this just overdesign, jumping on the bandwagon????)
We’ll see where it leads. I’m getting frustrated because I want to get on and have a working script to do what I need (and in my mind I can see exactly how it’ll work; how the interface will look etc) yet I’m not going anywhere because I want to get the design right. Which if I’m not careful may mean I never get anywhere, as the design’s never right 
Here’s a couple more resources:
* “Eclipse Library documentation”:http://www.students.cs.uu.nl/people/voostind/eclipse/api/Iterator.html on their Iterator class.
* “Harry’s page”:http://www.phppatterns.com/index.php/article/articleview/50/1/1/ on the subject
* “A forum posting”:http://www.devnetwork.net/forums/viewtopic.php?t=8532 suggesting an alternative to Harry’s approach. I liked this quote: ??it doesn’t take into account the fact that PHP is a very dynamic language, and doesn’t need to rely on methods built for languages like C++ and Java (strongly typed languages).??
Finally, in the same vein as the quote above, a quote from “PHP Everywhere”:http://php.weblogs.com
bq. Here’s an example of something that is very hard to implement in VB.NET or C# or Java: PHP can dynamically recompile itself; the famous Smarty template compiler being just the tip of the iceberg. There is a world of PHP innovation out there beyond cloning PHPNuke or J2EE, just waiting to be discovered!
Some things are good to copy, some things aren’t. Some patterns are good for PHP, some aren’t. In 20 years time I’m sure we’ll all know which; at the moment we’re still developing the best approaches to take with dynamically typed languages.
September 21st, 2003 — PHP
I’ve been working on an RSS aggregor (written in PHP) recently, similar to “Feed On Feeds”:http://minutillo.com/steve/feedonfeeds/, but with a slight twist. But what I’m doing’s not important for the rest of this post.
Where do you draw the line at too many classes? I’ve been trying to put some thought into the design of the code (not something I usually do) but yet again it seems to have caused more grief that it warrants.
I started by thinking which bits should have their own classes. I began with the design:
bc. Class Feed ($feed_url)
update()
classify()
Then I thought it would make more sense to look at the feeds _in terms of items_. After all, once they’re stored in the database I don’t care about the feed, only the individual items. This led me to:
bc. Class Feed ($feed_url)
update() - which instantiates for each item
bc. Class Item (&$data)
classify()
save()
reclassify()
delete()
Yet this still doesn’t seem a satisfactory design. Not only does it seem like lots more overhead due to PHP having to handle all these classes, but *I really can’t see the benefit over a more general structure* - i.e. just one class that I apply to _all feeds_ in turn (as opposed to instantiating a separate class for each feed), or even (dare I say it) *functions*. Perhaps someone can enlighten me where I’m going wrong. We haven’t touched on OO design yet at University (and thanks to the organisation of my course, I probably never will), so I’m swimming in the dark.
Over to you guys.
September 11th, 2003 — PHP
Anyone got a good way to remember when to use && and when to use || in an expression? I keep on using them the wrong way round - just spent an hour debugging a script, typecasting everything… and then found this was the problem:
Spotted the mistake (Hint: the first wrong character is @|@, and the last is @|@)?
Trouble is, when I use English, I do use an OR in that situation:
“If COST_USD or COST_GBP don’t equal $amt, then fail”
So if anyone’s got a mnemonic or similar that can help me remember this, please let me know. Otherwise, I’ll just have to wait until my brain’s as twisted as that of a CS(Computer Science) student 
September 11th, 2003 — PHP
No-one quite seems to know what PayPal have done to their IPN script, but at the moment if you POST over a non-SSL channel (i.e. port 80) you get a 302 message returned. PayPal _say_ that PHP is the only programming language having problems at the moment, but I’ve not tested any others.
I only found out after a couple of hours trying to debug why the payment interface for the new site I’m building wasn’t working properly (debugging done by emailing the contents of every socket transaction to myself - wish there was an easier way). There was nothing on PayPal’s site about 302 errors, and I’ve never come across them before, so I turned to google. This immediately turned up “this thread”:http://www.paypaldev.org/topic.asp?TOPIC_ID=2974&whichpage=1.
I then tried using SSL connections (fsockopen on ssl://www.paypal.com and port 443). Unfortunately this didn’t work either - I was getting an unknown socket error. I think my host has SSL support compiled in properly (it appears as –with-ssl in the compilation arguments, but nowhere in the body of a PHPInfo page) so I’ve no idea what the problem is.
Finally I resorted to using “cURL”:http://uk.php.net/curl. Wish I’d done so earlier, as it worked perfectly. Bravo for cURL and its developers!
August 21st, 2003 — PHP
Dear reader,
If you wrote the XML spec then I want you to know that I think you made a ******* bad job of it. Fancy missing out @ @ by default and making us have to use @ @ instead. Did you realise the problems this would cause parsing valid XHML pages?
OK, why the rant? Well, I’ve just spent 2 hours trying to track down a problem while using FormProcessor, and have just found it:
@
@
Yes folks, that’s what’s wasted so much time. One measily entity that isn’t included by default.
Bah.
Oh, and more details on the reasons behind this problem in this “PHP bug report”:http://bugs.php.net/bug.php?id=15092.
August 21st, 2003 — PHP
My current project is the biggest site I’ve had to design and build by myself. And in the process of planning the code I’ve realised why so many people have a framework they use. I’m having to do too much stuff here from scratch; code that should just plug in from existing projects. I’ve written a SQLBuilder library, polished up my own take on Fusebox, and am developing the FormProcessor library (the more I use it the more I realise why people do forms the “normal” - i.e. writing PHP to construct them - way: it’s such a pain writing these forms by hand. Dreamweaver really doesn’t speed up the generation much - I will post ideas of a dedicated application I think _someone else_ ought to write - sometime!).
So I’ve decided I need to invest time learning an existing framework, rather than write my own half-baked version. *Please*, if you’ve had good or bad experiences of frameworks, *post a comment*, and share your experience.