Link to home
Start Free TrialLog in
Avatar of jaxstorm
jaxstormFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help me "get" Object Orientated PHP

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

ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaxstorm

ASKER

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
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.
"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?
It'll do
I wrote two different articles on OOP programming in PHP:

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

https://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...