Solved

Help me "get" Object Orientated PHP

Posted on 2011-03-10
6
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 35

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

751 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