Solved

Help me "get" Object Orientated PHP

Posted on 2011-03-10
6
251 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

15 Experts available now in Live!

Get 1:1 Help Now