Forums: Back End:

 

PHP Class help (I'm a NOOb)

first
 

JERKSTORE PHP Class help (I'm a NOOb)

Okay, I've never delved into OOP with PHP before, but I've got a project right now that seems to present some opportunities to at least dip my toes in the water a bit a get a feel for it.

One such instance is a widget of sorts that will be used throughout the site. Basically, it's a listing of "related events" to present more info to the user.

It'll be re-used in several places, and depending on the usage, will need to show a certain number of items. So I figured I'd make a RelatedEvents class, pass the number items I want to feature into the class and let the magic begin.

Here's my existing (probably vomit-inducingly bad) code. I realize it's probably horrible, horrible practice to include some actual markup in the class itself as I've done - that's part of my question:


<?php
class RelatedEvents {
var $display_num;
var $rel_id;

function RelatedEvents($dnum,$rid){
$this->display_num = $dnum;
$this->rel_id = $rid;
# remember to use '$this->varname' when referring to variables in the class
?>
<div class="fixed-block">
<h2>Related Events <?=$testvar?></h2>
<div class="fixed-content">
<ul>
<li>
<p>info here...</p>
</li>
<li>
<p>info here...</p>
</li>
<li class="last">
<p>info here...</p>
</li>
</ul>
</div>
</div>
<?
}
}
?>


This works, on the very basic level of displaying the code on the page and being able to reference the variables in the constructor. However:

1) Am I a dumbass for including the markup in the Class itself? If that's bad news, what is the best practice for that - a class that basically serves to create markup.

2) Eventually, there will need to be a query associated with the class where the database is asked for x number of items (based on $display_num) relating to $rel_id. Can I include the query in the class itself? Currently, outside variables don't seem to work -- if I set $testvar outside of my class in the main PHP file, $testvar as shown in the class markup doesn't show display anything. So obviously I have to worry about the scope of things not a part of the class. So I'm assuming this will affect my ability to access my db connection and run a query in the class. How should I accomplish this?

3) Is this a stupid use of OOP? Am I better just doing something like:

$display_num = 2;
$rel_id = 5;
include "include_file_with_query_and_markup";


Any insight or guidance is greatly appreciated, since I have no idea what I'm doing.
smile

 

the real me

the real benefit of doing stuff the OOP way comes in when they all start tying in together. ideally you would have other classes that you would delegate part of the work of generating that list. like getting the data etc...

honestly if that's going to be your only class in the project you might be better off just using an include. smile

 

JERKSTORE

Thanks. I figured this probably wasn't the best case to make use of a class, but it was just repetitive enough to where I could kind of see a glimmer of how "oh hey this could be useful."

But I get what you're saying, so I'll just stick with doing what I know.

Appreciate the input smile

 

DontBogartMe

I reckon you should keep at it if you have the time and inclination to do it. It never hurts to learn a new trick or two...

I don't have time to go through the issues with you in any depth here, but here are some quick thoughts....


Firstly it's been a long time since I did OOP in PHP and I don't rem the syntax or what the limitations are....

I think your idea for a RelatedEvents class is sound enough. You'll have some code that is bulding up your webpage, and that code will create an instance of RelatedEvents and then it will use that instance, or object, to get at the RelatedEvents data.
So what will the class RelatedEvents need to be able to do?

- Connect to the DB
- Fetch the RelatedEvents out (perhaps restricted by some parameters?) of the DB
- Give the calling code one Event at a time.

The calling code would then do this:
- Create an instance of RelatedEvents.
- Give it the info it needs to connect to the database (you could pass it the db connection perhaps)
- Give it the order to fetch the RelatedEvents data from the database (pass in any parameters you need)
- Ask it how many events there are, and loop thru them, pulling out one event at a time and then displaying it in HTML.

Follow that and you won't need to mix up your HTML in your class - all the HTML bits will be in the calling code.

I don't know how complex each event is here, but you might also consider creating a second class RelatedEvent which just holds the data for a single event. Then when RelatedEvents gives back an event, it can give back a RelatedEvent object, making your methods a lot easier to write.

Hope that helps somewhat - sorry I can't give you any actual code.

 

JERKSTORE

DBM - I appreciate the response.

I think I understand what you're saying, up until the "calling code".

By calling code are you referring to the actual instance of the class like so:


$myEvents = new RelatedEvents(4,125,$connection);
// 4 is the number of results I want returned
// 125 is the id to query upon
// $connection is the DB connection

?

If so, I'm not understanding how I can separate my HTML out of the class. Wouldn't it still have to be in the class to take the DB info and write out the markup? I basically want to call the class (like above) and have that one call generate all of the markup. If my HTML isn't in the class, how do I create it?

 

DontBogartMe

yeah, that's what I meant by calling code.


The class has methods that just returns data about an event, e.g. getEventName which would return 'Been shopping' or whatever. Then in the calling code you'd wrap that string in HTML - therefore the HTML is in the calling code, the data is in the class.

 
first
 

Forums: Back End: PHP Class help (I'm a NOOb)

 
New Post
 
You must be logged in to post