Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help me "get" Object Orientated PHP

Posted on 2011-03-10
6
Medium Priority
?
294 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 1000 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

824 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