Programming Tools
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingProgramming Tools

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 16th, 2003, 12:39 AM
benos benos is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 233 benos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Article Dicussion: Designing Classes in PHP

If you have any questions or comments about this article please post them here.

This forum post relates to this article

Reply With Quote
  #2  
Old May 22nd, 2003, 07:39 PM
assemblage assemblage is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: North Augusta, SC
Posts: 13 assemblage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Accessing class objects within classes?

Great article. Really super! It's really helped me understand about classes.

Unfortunately it only shows how to create static html pages using PHP. I wanted to add some dynamic parts to it such as news and reviews that are pulled from a mySQL database. That's one reason why I'm a PHP and mySQL fanboy. So I worked on it.

Learning about PHP in the past year, I've read through many articles on this site, Devshed and Sitepoint. A while ago I came across PHP and Databases for the Lazy Sod by Justin Vincent . I read it and worked through it, but didn't understand what he was doing because I didn't understand OOP and PHP classes. Because this article taught me so much about classes, I've been able work through Justin's article again and understand the code in his class decently enough to write my own version of his class. It just doesn't have all the fancy stuff his class has. What I wrote just hooks up to the database. That saves me from having to rewrite (cut 'n paste) all those database hookup lines. I guess I'm a Lazy Sod Taking what I learned from this tutorial and Justin's I've been able to combine them and create some dynamic pages like I want. I guess that’s the real measure of learning… being able to apply the knowledge.

I combined them and created my dynamic version by creating two 'class objects' ( I guess that's the correct terminology ). One object is for the page template and one for the database connection. This is coded in my index.php page. I created objects for both classes in my index.php page and pass the database connection class object to functions in my page template class object (not sure if that makes much sense ). For example to draw the news section of my page I wrote this in my ‘index.php’ file:
Code:
  require ('includes/class_dbconn.php'); //the object $dbconn is assigned in this page
  require ('includes/class_page.php');
  $homepage = new Page();
  $homepage->news($dbconn); 


My ‘class_dbconn.php’ page is my modified version of Justin’s class. It looks something like this:
Code:
<?php
define("DATABASE","mysecret");
define("USER","mysecret");
define("PASS","mysecret");
define("HOSTNAME","mysecret");

class dbconn
{
  function dbconn ($database,$user,$pass,$hostname)
  {
    $this->connection = mysql_connect($hostname, $user, $pass);
    $this->selectdb($database);
  }
  
  function selectdb($database)
  {
    mysql_select_db($database, $this->connection);
  }
}
$dbconn = new dbconn(DATABASE,USER,PASS,HOSTNAME);
?>


My ‘class_page.php’ file is a modified version of the code in this tutorial. I added a ‘news’ function. It looks something like this (minus a lot of other code for the rest of the page, I don’t just have news on my site ):
Code:
function news($dbconn)
{
  $query="SELECT newsheadline, newscatch FROM news LIMIT 10";
  //DATABASE a constant that is in the class_dbconn.php file outside of the class
  $result = mysql_db_query(DATABASE, $query, $dbconn->connection);
  $newsbox='';
  while ($row = mysql_fetch_object($result))
  {
    $newsbox .= "<p>$row->newsheadline<br>$row->newscatch</p>;
  }
  echo $newsbox;
}


It works! But is this the best way? My definition of ‘best’ in this context means the best in logic, style, and overhead or processing requirements.

1) If I do it this way should I be passing a 'reference'? If so how do i do that? I'm thinking that it's the '&' or maybe the '@'.

2) I know that every time I call this page I'll want to call the database connection class (from class_dbconn.php that I assign to $dbconn). I guess I can have two classes in an include file which means I don't need that extra line of code in my 'index.php' file for the 'class_dbconn.php' include. Do I want to do that or am I doing good so far?

3)My big question is can I access the $dbconn class from the $page class? That way I'm not passing the $dbconn object. I don't know if that matters at all though. For example:
Code:
function news() //took out the $dbconn.. only change
{
  $query="SELECT newsheadline, newscatch FROM news LIMIT 10";
  //DATABASE a constant that is in the class_dbconn.php file outside of the class
  $result = mysql_db_query(DATABASE, $query, $dbconn->connection);
  $newsbox='';
  while ($row = mysql_fetch_object($result))
  {
    $newsbox .= "<p>$row->newsheadline<br>$row->newscatch</p>;
  }
  echo $newsbox;
}


Any help would be appreciated. My logic may be flawed but I've just figured out this class stuff this week. I've been trying to figure it out for months and this article just turned that light on. I’m ok with procedural programming, but this OOP stuff has been confusing to me. But when that light turns on it’s just great. I love it when that happens. My site may be lucky to get 50 visitors a month, but I never know. I might get a million. I just want my programming to be good stuff. I don't want to write sloppy and inefficient code and be happy with it just because it works.

Thanks in advance for any help and thanks again for this article.

Reply With Quote
  #3  
Old June 11th, 2003, 05:19 PM
assemblage assemblage is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: North Augusta, SC
Posts: 13 assemblage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Was hoping for more

Was hoping I could get a little more help than this. But I guess it sure doesn't help that the article has been deleted or something. WTF?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingProgramming Tools > Article Dicussion: Designing Classes in PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT