Solved

Help me "get" Object Orientated PHP

Posted on 2011-03-10
6
258 Views
Last Modified: 2016-02-14
I'm a reasonably decent PHP programmer but I just don't "get" the object orientated side and it's high time I sorted it out. This may not be worth 500 points but I thought I'd try anyway.

In the code snippet I have some code I've written to pull some info from a mongoDB. I realised I've used some classes in there but I haven't constructed any - they're just what the MongoDB driver provides.
In the foreach loop I have some variables that are printed out from the MongoDB. I'd like to make that foreach loop a class so that I can call something like:

print->Environment
print->Architecture

which makes it nice and clean and simple. I figure if someone can help me create a class from something I've already written it might help me "get" classes and objects. All help gratefully considered.
$gethost=$_GET['q'];
try {
  // open connection to MongoDB server
  $conn = new Mongo('cbb-cs03-bt26-17');

  // access database
  $db = $conn->factdb;

  //authenticate
  $db->authenticate('username','password');

  // access collection
  $collection = $db->hosts;

  // define what to find
  $host = array(
        'host' => $gethost
);
  // disconnect from server
  $conn->close();
} catch (MongoConnectionException $e) {
  die('Error connecting to MongoDB server');
} catch (MongoException $e) {
  die('Error: ' . $e->getMessage());
}


 $cursor = $collection->find($host);
  foreach ($cursor as $value) {
          echo '<tr><td><b>Environment</b></td><td>'.$value['facter']['environment']['value'].'</td><td>'.date("M j Y",$value['facter']['environment']['created_at']).'</td><td>'.date("M j Y",$value['facter']['environment']['updated_at']).'</td></tr>';
  echo '<tr><td><b>FQDN</b></td><td>'.$value['facter']['fqdn']['value'].'</td><td>'.date("M j Y",$value['facter']['fqdn']['created_at']).'</td><td>'.date("M j Y",$value['facter']['fqdn']['updated_at']).'</td></tr>';
  echo '<tr><td><b>Model</b></td><td>'.$value['allocations']['model']['value'].'</td><td>'.date("M j Y",$value['allocations']['model']['created_at']).'</td><td>'.date("M j Y",$value['allocations']['model']['updated_at']).'</td></tr>';



}

Open in new window

0
Comment
Question by:jaxstorm
  • 3
  • 2
6 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35096658
Have a read of this http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3212-Using-PHP-classes-to-group-data-in-memory.html It illustrates a use of OOP that is often unregarded.

The key to OOP is to consider the operations you want to carry out and the data needed to support those operations, so in the example you want to consider, what are the functions you need to have? What information to you need to store? What are these

print->Environment
print->Architecture

supposed to do or produce? Answer that and we can make a start.
0
 
LVL 8

Author Comment

by:jaxstorm
ID: 35096753
I'd like print-> Architecture to take a variable defined by

$value['facter']['environment']['value'] from the foreach loop and print it inside <td> tags essentially.

$value is defined by
$cursor = $collection->find($host);
  foreach ($cursor as $value) {
}

essentially I want a "print" class that has functions within that gets the values of array $value['factor']['environment']['value'] (replace environment with array values where necessary
0
 
LVL 8

Author Comment

by:jaxstorm
ID: 35096788
Just ftr bportlock, I'm going to read that article because it looks brilliant, exactly what I need to do but my brain is a bit frazzled today so will take a look tomorrow.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35110402
"I'd like print-> Architecture to take a variable defined by

$value['facter']['environment']['value'] from the foreach loop and print it inside <td> tags essentially.

$value is defined by
$cursor = $collection->find($host);
  foreach ($cursor as $value) {
}"


Hmmm.... a class for this already exists - its instance is called $collection. Is there a reason why you're Mongo as opposed to MySQLi or PDO?
0
 
LVL 8

Author Closing Comment

by:jaxstorm
ID: 35375886
It'll do
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41463928
I wrote two different articles on OOP programming in PHP:

http://www.experts-exchange.com/articles/2626/Beginning-Object-Oriented-Programming-in-PHP.html

http://www.experts-exchange.com/articles/2631/Advanced-Object-Oriented-Programming-in-PHP.html

...and I just realized that the timestamp on this question is from 5 years ago. Strange that it just showed up in my feed...
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now