Home › Forums › Languages › PHP & MySQL › OOP Tutorials
This topic contains 8 replies, has 4 voices, and was last updated by Anonymous 1 year, 7 months ago.
-
AuthorPosts
-
June 11, 2011 at 12:02 am #13885
AnonymousCan someone with knowledge create some OOP tutorials that actually reflect something that I would actually use? I found some that are just gibberish because they are talking about things that I would never actually use while programming.
June 13, 2011 at 7:43 pm #14638OOP is a very complex subject, there is a lot to it, which is why it can’t be discussed in a mere tutorial. What are you trying to do? Why do you want to use OOP to achive that? Are you sure it isn’t an overkill? Very simple scripts do not need to use OOP. Or if they do, this is generally done for educational purposes.
Other than this, I could try to write up a tutorial for one or more aspects, depending on what you have in mind. So please be more specific.
June 18, 2011 at 7:38 pm #14639
AnonymousWhat I really need is a tutorial to understand the basics. But I need something that I would actually use. A tutorial in the context of cars or dogs or whatever just doesn’t WORK for me. I’ve tried numerous times.
At the moment, I’m not trying to write a script using OOP, but to just UNDERSTAND it. So if that can be done in the context of say preparing to write a link directory script or something, it would be most helpful.
June 18, 2011 at 8:25 pm #14640Well link directory:
Example:
You create a class called “Link”
Every instance of that class is an Object.
So a link might have the following properties (this is just pseudocode, not an actual language
):Link {
Name
Url
Description
Button
}
Then when you add a link to the directory, you’re creating a new instance.
Link myLink = new Link(name,url,desc,button)
—-
Combined with the OO tutorials you have read, does this make sense?
June 19, 2011 at 1:09 am #14641
AnonymousIt does. I think the biggest issue I’m having is transferring what I learn in tutorials to do what I want to do with it. For example, I followed this tutorial (http://www.killerphp.com/tutorials/object-oriented-php/) but based on that, I have no idea how take that and translate that into making a link directory script.
June 19, 2011 at 2:04 am #14642Ok, so you have link objects.
Directories also have categories.
So a way of doing it could be category objects as well.
A category has a name, a description and a list of link objects.
Category {
string Name
String Description
ArrayList<Link> Links
}
So you could create a category
Category aCat = new Category(“tutorial sites”,”they teach you stuff”)
then you could do like
acat.add(new Link(“Life as rose”,”http://lifeasrose.ca”,”some blog”));
Objects have methods (functions), associated with them and in this case I just used the “.add” so I’d have to write this into my category class;
Category {
string Name
String Description
ArrayList<Link> Links
function add(Link toAdd) {
Links.add(toAdd);
}
}
Then, when you’re done updating the category maybe you’ll call
category.save()
which will call a method you wrote which saves this back to the database.
June 21, 2011 at 11:54 pm #14643
AnonymousI really appreciate your time, Rose, trust me but most of what you said above is gibberish to me. I literally need step by step instructions with the actual code that I would use to make it all work. I don’t need someone to write the whole thing but just a really simple example of an entire working product. A link directory script is obviously very complex but it COULD be simple.
I would take any kind of example. A CodeGrrl script I used to love was just a script where you added things and it would list them alphabetically (I think it was called simplealpha). Anything like that would be great.
I don’t even know how I would connect to a database with OOP. I’ve attempted to look at PHPASKIT but that must be too complex for me. I feel like a huge dope but this is something I’ve attempted on/off for a couple years. I’m very annoyed with myself.
Once again, thank you for attempting to help me.
June 22, 2011 at 2:44 am #14644Basically, a class is a collection of methods that you can reuse. The methods in each class are meant to interact with each other. So, for example, a class to deal with listing links would need methods to save links and to retrieve them.
As an example, OOP in PHP5:
class Link{
public $url, $title, $description;
public function __construct($url, $title, $description)
{
$this->url = $url;
$this->title = $title;
$this->description = $description;
}
public function save()
{
$url = mysql_real_escape_string($this->url);
$title = mysql_real_escape_string($this->title);
$description = mysql_real_escape_string($this->description);
mysql_query("
INSERT INTO links
(url, title, description)
VALUES ('$url', '$title', '$description')
");
}
}
$link = new Link('http://example.com/', 'Example Title', 'Description');
$link->save();
When you create a
new Link, you’re assigning an object to the$linkvariable. The arguments passed are “magically” run though the__constructmethod. Then, from within the__constructmethod, the class’s variables are assigned, which can then be reused later. The$linkvariable can access any of the class’s variables or methods that havepublicvisibility. If you assigned visibility asprivateorprotected, you could only access them from within the scope of the class.While working within the scope (i.e., inside a class’s method), the
$thisvariable references the class itself. So, while you can use$link->urloutside of the class, you would use$this->urlinside of the class.So, in the above example, you create a new link, assign the variables, then save it to the database. If you so desired, you could create another link by reassigning the variables and running the
savemethod again:$link->url = 'http://anothersite.com/';$link->title = 'Title';
$link->description = '';
$link->save();
You could write more methods to support retrieving the data, looping through it, updating it, whatever you want. For example, you may add another variable to the class, called results:
private $results;It’s private because it’s meant to be used only inside of the class. Then, a couple methods to retrieve the data:
public function have_links(){
$this->result = mysql_query("SELECT * FROM links");
return mysql_num_rows();
}
public function more_results()
{
return mysql_fetch_assoc($this->result);
}
You can easily get it and loop through it like so:
if ($link->have_links()) {while ($row = $link->more_results()) {
// Do stuff for each row
}
}
There are tons of things you can do to improve this. The idea is that you can separate the class from the HTML output, allowing you to keep your code nice and tidy.
If you choose to work in PHP, OOP is best supported in PHP5. PHP4 started to, but it’s clunky and annoying (opinionated, of course). Tons of other languages also support objects, so it’s just a matter of choosing your favorite and knowing that it’s nothing more than a bunch of reusable functions and variables that are meant to keep code clean and accessible.
October 10, 2011 at 2:18 am #14645
AnonymousBeen working on this ever so slowly. A lot of what you knowledgeable people is no longer complete gibberish, so I believe I am making progress!
-
AuthorPosts
You must be logged in to reply to this topic.



Recent Comments