Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

OOP Advice


Demons Halo's Avatar
Member
0 0

Hi guys, I've wanted to take OOP on for a while now, but I just can't get my head around it. Procedural programming (for me) is a lot easier to understand, so I'd appreciate it if I could get some help/advice from someone more experienced :P

I've started my first PHP OOP project, which is a chart drawing script. I haven't completed the project, yet still I'd like to get some feedback before I continue.

The Scripts is supposed to do the following:

take in:

  • chart name
  • chart resolution
  • X axis name
  • Y axis name
  • X and Y coordinates for 3 bars. (bar name and color will be added later)

display:

  • chart logo, axis names and axis points
  • 3 bars on top of the charts

Now I haven't started working on the bars yet, but I've managed to create a chart where the X and Y axis are drawn with 0 as starting point, and up to any user specific number on both X and Y axis.

a link to my project can be found here: http://www.demonshalo.com/chartiz/index.php

OOP source can be found here:http://www.demonshalo.com/chartiz/oop.rar

Procedural source: http://www.demonshalo.com/chartiz/procedural.rar

both sources do almost the same thing, yet still the OOP was a bit longer (consisting of 2 files so far) and was a bit more confusing to write…

To get a proper charts drawn, you need to fill in at least X and Y coordinates for 1 bar so that the maximum axis length can be set.

The problem is that procedural programming is a bit forward, you program whatever you see come next. But the OOP mindset confuses me a little bit. I seem to use classes as **containers **for functions, which seems like a waste since I can get the same job done in less code when using non-OOP.

How do I reach the OOP full potential? Is there a certain way to think before constructing a program?

Thanks in advance?


spyware's Avatar
Banned
0 0

Demons Halo wrote: How do I reach the OOP full potential?

Shiiiiiiit.

No one knows.


Demons Halo's Avatar
Member
0 0

spyware wrote: [quote]Demons Halo wrote: How do I reach the OOP full potential?

Shiiiiiiit.

No one knows.[/quote]

I guess if you don't then it's true, no one knows :/


spyware's Avatar
Banned
0 0

Demons Halo wrote: I guess if you don't then it's true, no one knows :/

Wat.

No, look. OOP probably has it's uses in large-scale projects with many repetitive tasks. I just don't like it.


Demons Halo's Avatar
Member
0 0

spyware wrote: [quote]Demons Halo wrote: I guess if you don't then it's true, no one knows :/

Wat.

No, look. OOP probably has it's uses in large-scale projects with many repetitive tasks. I just don't like it.[/quote]

The problem is that everybody use it now a days. so if you dont understand it, it's freaking hard to follow up and understand the structure of the programs written by others!


stealth-'s Avatar
Ninja Extreme
0 0

Object oriented programming makes much more sense in some situations compared to others. I wouldn't consider myself anywhere near "good" at understanding and using OOP, but I've met quite a few situations where it can be very useful. I've never looked at utilizing it in PHP, as I've always focused a bit more on python for OOP related stuff, but when you refer to a class just being a container for some functions, it can be a lot more than that. The class instances can also hold variables, which is what makes OOP very useful in that context.

A great example is, say you're building a network scanner. The user wants to scan 192.168.0/24. Doesn't seem too hard, but say, we want to scan every port, and we want to keep information on each host. Examples of that information would be what it's DNS resolves to, the whois report, etc. As you can see, it would become a little difficult to manage all of that. With OOP it's as simple as creating a class that has the attributes (variables) ip, dns, whois, etc. Then you can make methods of that class that do things related to the class, such as LookupWhois, and Scan, rather than having it as a outside function. You then create 256 instances of that class, and boom, the scanner code got a lot simpler. This is also far more elegant and easy to understand, at least in my opinion.

Another great feature of it is inheritance. This makes working with libraries and large amounts of code amazingly easy. Each class you create can "subclass" another class. By doing that, all the methods and information are also pulled into your class, and then you just "overwrite" what you feel like. For instance, in python, making a threaded application is as easy as:

class mythread(threading.Thread):
        def run(self):
              while 1:
                    print "Hi"
instance = mythread()
instance.start()```

We never created a start() method, but we call it, and we can because that was inherited by the threading.Thead class. That way, the programmer doesn't have to worry about any threading code or anything, because he basically just steals most of it and writes over the part he wants to do his way. That's much easier than the non-OOP way, imo.

The problem with all of OOP is it's really fucking complicated compared to non-OOP until you manage to wrap your head around it, but when you do it looks far simpler and way more elegant. So, I'll basically just sum up and tell you what I tell everyone else: If you *really* want a good understanding of OOP, go get a book, and spend lots of time thinking about where it would be really useful, and implement those ideas.

ghost's Avatar
0 0

The problem is that procedural programming is a bit forward, you program whatever you see come next. But the OOP mindset confuses me a little bit. I seem to use classes as **containers **for functions, which seems like a waste since I can get the same job done in less code when using non-OOP.

How do I reach the OOP full potential? Is there a certain way to think before constructing a program?

are we talking here about OOP or brain surgery

what is there to be confused about ?

you group some functions and structure/variables togethet and give that grupe a name and you have yourself a class, or im a living in a delusion ???


Demons Halo's Avatar
Member
0 0

stealth- wrote: Object oriented programming makes much more sense in some situations compared to others. I wouldn't consider myself anywhere near "good" at understanding and using OOP, but I've met quite a few situations where it can be very useful. I've never looked at utilizing it in PHP, as I've always focused a bit more on python for OOP related stuff, but when you refer to a class just being a container for some functions, it can be a lot more than that. The class instances can also hold variables, which is what makes OOP very useful in that context.

A great example is, say you're building a network scanner. The user wants to scan 192.168.0/24. Doesn't seem too hard, but say, we want to scan every port, and we want to keep information on each host. Examples of that information would be what it's DNS resolves to, the whois report, etc. As you can see, it would become a little difficult to manage all of that. With OOP it's as simple as creating a class that has the attributes (variables) ip, dns, whois, etc. Then you can make methods of that class that do things related to the class, such as LookupWhois, and Scan, rather than having it as a outside function. You then create 256 instances of that class, and boom, the scanner code got a lot simpler. This is also far more elegant and easy to understand, at least in my opinion.

Another great feature of it is inheritance. This makes working with libraries and large amounts of code amazingly easy. Each class you create can "subclass" another class. By doing that, all the methods and information are also pulled into your class, and then you just "overwrite" what you feel like. For instance, in python, making a threaded application is as easy as:

class mythread(threading.Thread):
        def run(self):
              while 1:
                    print "Hi"
instance = mythread()
instance.start()```

We never created a start() method, but we call it, and we can because that was inherited by the threading.Thead class. That way, the programmer doesn't have to worry about any threading code or anything, because he basically just steals most of it and writes over the part he wants to do his way. That's much easier than the non-OOP way, imo.

The problem with all of OOP is it's really fucking complicated compared to non-OOP until you manage to wrap your head around it, but when you do it looks far simpler and way more elegant. So, I'll basically just sum up and tell you what I tell everyone else: If you *really* want a good understanding of OOP, go get a book, and spend lots of time thinking about where it would be really useful, and implement those ideas.

Thanks for the explanation =)
The scanner was indeed a good example of an oop project. 
Now that I think about it, OOP will help me out when I'm about to create multiple bars for the charts. So I can create an instance for every bar and have its' info stored inside =D That way I can ignore using for loops!

I'll try it out. Thanks for the help =)

stealth-'s Avatar
Ninja Extreme
0 0

Demons Halo wrote: Thanks for the explanation =) The scanner was indeed a good example of an oop project. Now that I think about it, OOP will help me out when I'm about to create multiple bars for the charts. So I can create an instance for every bar and have its' info stored inside =D That way I can ignore using for loops!

I'll try it out. Thanks for the help =)

No problem, good luck with your project :)

@mestar

It's kinda like database designing. It doesn't seem that complicated, but when you get into keys and weaving different tables and databases together, stuff gets a lot more advanced. Then people start writing books on the theory, how to best utilize it, the most elegant designs, etc. and all of the sudden database designing isn't so straightforward. At least that's how I look at it.