TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-21-2007, 09:56 PM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default My Code?

I'm in the middle of coding a script, and before I want to continue, I want to make sure that my OOP at least conforms to what it should look like. Also, I noticed Haris has posted threads asking if his code looks good, so I figured it's worth a shot to post mine.

Keep in mind, this isn't nearly finished, I still have the whois function to make, and then also add in other features I have planned.

Index:
http://psdtocode.com/index.phps

Whois Class:
http://psdtocode.com/whois.class.phps

A brief explanation on my whois output. I'm a neat freak when it comes to how my code looks, so I wanted to output it to look perfect. However, if I release this (probably unlikely), I'm going to get rid of that so it doesn't look weird in different layouts.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 10-22-2007, 10:36 AM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Nice start Andrew, there's a few places where I'd like to point out some mistakes. The first mistake is your lack of member declarations, you have assigned a value to 2 member properties but there is no actual declaration for either property. Example:

PHP Code:
class Whois
{
   private 
$domain;
   private 
$extensions;
   ... 
The visibility of those properties may need to be public or some accessor methods adding (that is, a get method for returning their values). The next thing I noticed was that you called validate and passed 2 arguments along with the call, even though:

1) valdidates does not accept any function arguments
2) the arguments you try to pass through are already stored
as member properties, thus you already have access to them
inside the valdiates function

Im guessing that was actually a mistake and wasn't intentional? Overall, nice start, good to see you trying to do this in OOP :)
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 10-22-2007, 11:20 PM   #3 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

What do you mean in your bullet points? Should I pass them into the validate function as they are ($this->domain)? Is that supposed to give validate ability to use them? Sorry, new to OOP. :) I also was thinking about declaring those variables, yet wasn't exactly sure what to do for them. Thanks for the tips, will put them in use. :)
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 10-22-2007, 11:32 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

A couple of things to mention. Concerning your regular expression for checking the domains, I don't think such domains as .co.uk and .org.uk domains will validate. Also the hyphen character (-) is technically a regular expression character for range and therefore should be escaped. Although it would work fine in the instances you've used to it in.

Also, one more thing, just for making your code nice and tidy if you're particular - which it would appear you are, use the single quotes (see this article) when outputting the HTML, or the heredoc concept (which I personally dislike) makes it much neater:

Single Quotes (Yum!)

PHP Code:
echo 'Show this';
echo 
'And this';
echo 
'And this, too!'
Heredoc (Yuk!)

PHP Code:
<<<EOF
Show this
And this
And this, too!
EOF; 
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-22-2007, 11:58 PM   #5 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

I did change it to single quotes, but the escaped characters for new lines and such didn't work, and I didn't want to have single quotes, then double quotes after the singles just to make those work.

Also, I was aware of those domains not working. I was talking with Salathe about it for a bit and realized it, which I still need to find a work around for.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 10-23-2007, 12:41 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Just a little bit of information which might help you. You probably know this already (or wish you did) but the explode function can have an optional third argument called limit. This enables you to limit the number of array elements returned. To put that into something visual:

PHP Code:
<?php

$domain 
'salathe.co.uk';

// Normal way of using explode
$parts_normal explode('.'$domain); // array('salathe', 'co', 'uk')

// Limit the number of elements
$parts_limit explode('.'$domain2); // array('salathe', 'co.uk')

// You could also do
$extension array_pop(explode('.'$domain2)); // $extension = 'co.uk'


// Show some output 
header('Content-Type: text/plain');
var_dump($parts_normal$parts_limit$extension);
Another point that I'd like to raise is that your objects (classes) should only really be doing one thing. Personally, I think that you Whois class should be dealing only with the whois information, not handling the output at all (the Whois::display() method).

If somewhere along the line you have 20 classes all with a display() method within them, it'll be a nightmare trying to debug the output and even worse if later on you decide that you want to offer both HTML and another form of output (XML, PDF, whatever).
Salathe is offline  
Reply With Quote
Old 10-23-2007, 01:33 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You also really want to check for if they enter the www. part of the domain which they could do, and act accordingly, else your extension is going to be incorrect.

PHP Code:
$domain 'www.salathe.co.uk';
$limit 2;

if(
substr($domain04) == 'www.')
{
    
$limit 3;
}

$extension array_pop(explode('.'$domain$limit)); 
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-23-2007, 03:49 AM   #8 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Thanks for all the help, I saw the limit part of explode on php.net, but never thought to look at it.

I'm actually thinking of dumping this project. It was a good idea I thought at first, but last night I was thinking maybe I'm doing too much trying to both learn OOP and even more techniques (doing whois with fsockopen), all at once. Thanks for all the help, and it will come in handy in the next project I'm planning. I'm doing to outline this one out first to make sure I have it all in my head.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 10-23-2007, 05:53 AM   #9 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

And you forgot the comments.

For including configuration and classes file, why not use include_once instead of include? That'll help producing error if you accidentally include them more than 1 time.
Haris is offline  
Reply With Quote
Old 10-23-2007, 01:44 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I did one of these a year or so ago, and although the code's not that good any more (edit: updated it :)), it does still work. It may come in handy if you do decide to continue.

PHP Code:
$szAddress sprintf('http://www.whois.net/whois_new.cgi?d=%s&tld=%s'$szDomainNoExt$szDomainExt);

curl_setopt($pCurlCURLOPT_URL$szAddress);
curl_setopt($pCurlCURLOPT_HEADERtrue);
curl_setopt($pCurlCURLOPT_RETURNTRANSFERtrue);
curl_setopt($pCurlCURLOPT_FOLLOWLOCATIONtrue);

$szData curl_exec($pCurl);
curl_close($pCurl);
    
if(
strpos(strip_tags($szData), 'No match found for'))
{
    return 
false;
}

return 
true
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-23-2007, 10:30 PM   #11 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Quote:
Originally Posted by Haris View Post
And you forgot the comments.

For including configuration and classes file, why not use include_once instead of include? That'll help producing error if you accidentally include them more than 1 time.
I went back through and did comments, but since I didn't update any of the code, I didn't reupload the source.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:24 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design