Jump to content

Good uses of OOP?


Boo-urns

Recommended Posts

Some uses of oop are easy to comprehend. Class for Users / Common Validation etc... Would it be a good idea to extend the mysqli class? To create queries and to return the loaded results. Along with that to create it for ease of use in the future should I send it fields / table / WHERE info or would that not be a good idea?

 

I just need a push :)

Link to comment
Share on other sites

OOP is just a bunch of methods (functions) and variables put in a single place.  Think about it like an fishtank or ecosystem of php code.

 

So do whatever you want with it.... drain the tank and put a TV inside if it makes you happy.  Once in a while its good to read a book or tutorial to get ideas and appreciate other intellects, but you really just need to get in there and tinker.  Having some kind of mission you want to accomplish always helps of course.  Good luck!

Link to comment
Share on other sites

Some uses of oop are easy to comprehend. Class for Users / Common Validation etc... Would it be a good idea to extend the mysqli class? To create queries and to return the loaded results. Along with that to create it for ease of use in the future should I send it fields / table / WHERE info or would that not be a good idea?

 

I just need a push :)

 

 

PHP doesn't force you to structure your OOP in any particular way. This leaves everything up to you. I use OOP to create a common structure that is somewhat suitable for just about every application.  Reusing this structure makes programming go much quicker because I'm used to do things with my own library of classes and functions. I also use it in conjunction with MVC to create separation between the markup, business logic, and data layers.

Link to comment
Share on other sites

OOP is just a bunch of methods (functions) and variables put in a single place.  Think about it like an fishtank or ecosystem of php code.

 

SOmehow I disagree with that point of view...

 

Mostly agreed, while it might be "a bunch of methods (functions) and variables put in a single place" there should be some organization/meaning behind why those functions/variables are there. Not everything needs to be in OOP format, I've seen quite a few good rules of when to use OOP... I'll quote some from one list:

  • When your design is large, or is likely to become large.
  • When types of data form a natural hierarchy that lets us use inheritance.
  • When the system design is already object-oriented.
  • When implementation of components is likely to change, especially in the same program.
  • When huge numbers of clients use the same code
  • When you have a piece of data on which many different operations are applied
  • When the kinds of operations have standard names (check, process, etc).

Link to comment
Share on other sites

Ok so i missed my moment ill post anyway:

 

OOP is just a bunch of methods (functions) and variables put in a single place.  Think about it like an fishtank or ecosystem of php code.

 

SOmehow I disagree with that point of view...

I agree, OOP is all about making the code easier to understand/reuse/debug/moidy/extend and use. Not too much about efficiency its more about time, the less time spent writig code the better, more money.

 

OOP is a hierachial structure of programming meaning you should have a single main class, of which other classes extend etc, eg, a main class of human, could have methods like eat, drink, walk, then body could extend the human class to give individual attributes like height width and weight.

 

EDIT: not finished lol, although PHP does not have FULL OOP support as C++ and most other low(high?) level languages have.

 

Hope this helps :P.

Link to comment
Share on other sites

OOP is just a bunch of methods (functions) and variables put in a single place.  Think about it like an fishtank or ecosystem of php code.

I think that's called namespace oop is more then that

 

It should be called that, once PHP 5.3 is out. As it is now, many people (including me) use classes as a stand-in for namespaces.

Link to comment
Share on other sites

Hey Boo-ums,

 

Since no one has sent you any examples yet, here are a few that I've written:

 

http://www.solutionbot.com/2008/12/27/secure-session-management/

http://www.solutionbot.com/2008/12/27/secure-file-upload/

http://www.solutionbot.com/2008/10/27/pdo-where-php-is-headed-php-data-objects/

 

The last one is an example of using PDO instead of mysql, or mysqli. This is the direction PHP is headed in the future. Mysql and Mysqli extensions are being phased into PDO (http://us2.php.net/manual/en/book.pdo.php).

Link to comment
Share on other sites

one class I made was to handle data recieved by a server script (which is just a socket and a loop) but it was pretty neat :) and then I extended that into a whole class for just starting/running a server..

 

and then I made a class for mysql connections to make it easier, and then I made a user class which handled data pertaining to a specific user in a members' only area..

 

there is millions of ways/reasons to use OOP, but there is some things which really don't need OOP like my user class or my server class..

 

you see,, OOP is exactly what its name states "Object Oriented"

 

objects are meant to be reinitialized, over, and over..

 

for example

<?php
class Pet {
  public $name, $age, $type;
  public function __construct($type,$name,$age) {
    $this->name = $name;
    $this->age = $age;
    $this->$type = $type;
  }
  public function getAge() {
    return $this->age;
  }
  public function __toString() {
    return $this->name.'('.$this->age.') is a '.$this->type;
  }
}
?>
$rosco = new Pet('dog','rosco',3);
echo $rosco->getAge(); // 3
echo $rosco; // rosco(3) is a dog

 

but also some people make classes so that they could package a script into a file which is suitable for opensource packaging, or reusability..

 

because with a class its about being a blank shell..

 

like plant pot

 

it has ALL the means to grow a plant..

 

you just need to plant the seed you want inside the pot, and apply some PlantPot::love() and PlantPot::waterThePlantPot() until it is completely as you want it to be.

Link to comment
Share on other sites

"OOP is just a bunch of methods (functions) and variables put in a single place.  Think about it like an fishtank or ecosystem of php code."

 

So, totally, incorrect.

 

OOP is a methodology for implementing functionality in a conceptual way.  It enables you program in the terms of your problem by separating design and implementation. It also allows for ease of re-use, redesign and distributed development.

 

One of the best books I've ever read on OOP/OOD is: http://books.google.com/books?id=FnNqkN17kDYC

 

Totally free online. Enjoy!

Link to comment
Share on other sites

Something to note. There are alot of other languages where understanding OOP is really not the issue. C#, Java, Python, you are an Object Oriented Programmer weather you know it or not. Its the best way of promoting reusable code. PHP started out as a Procedural Oriented language. There is alot of debate between the two. Object Oriented programmers would say that working with OOP is better because it is easier to create and maintain large enterprise systems that many developers work on together. It's all about the code readability and ease of use and reuse. Nothing redundant. On the other side of the spectrum, we have our procedural programmers who are all about performance. OOP causes alot of 'unnecessary overhead' that reduces performance. Procedural programming opts more for performance and focuses on creating user-defined functions or better yet language extensions (in this case c++ for PHP) that do not use OOP. There are compelling reasons to do both. You can definitely tell that PHP tends towards Procedural programming as there are over 3,000 built in functions. It is moving more towards OOP, and I personally believe this is a good thing, but there are a lot of compelling reasons to not overdo the OOP. PHP is very powerful in that you have more of a choice in what kind of developer you wish to be. However with that vast flexibility there is a price to pay. No one PHP developers code looks exactly the same as another. This is why there are so many frameworks out there such as Zend and Symphony. The idea is not only to create a rapid application development framework (RAD) but also to create boundaries. Boundaries are important when working in a multideveloper environment as no one would be on the same page about anything. What I like about PHP is the ability, power and flexability to define my own API and framework. You can do this in very unique ways as there are much fewer boundaries in PHP.

 

Just a few random thoughts.

Link to comment
Share on other sites

"There are alot of other languages where understanding OOP is really not the issue. C#, Java, Python, you are an Object Oriented Programmer weather you know it or not"

 

Man, Wrong, wrong, wrong!

 

 

Programming in an OOP Language does NOT equal programming in an OOP Way!

 

OOP is a methodology which requires alot of practice and thought to do correctly and efficiently.

Link to comment
Share on other sites

Wow thanks for all the insights about OOP. Along with the links to some tutorials.

 

I've been through a few tutorials so far and have probably just touched the surface of OOP but want to dive much deeper so I can create some very reusable code in the future and possibly create a framework (in time). I'll most likely have more questions in the future but thanks for the push guys!

Link to comment
Share on other sites

Using the the OOP approach is awesome, but people trying to learn OOP always get it wrong, but it's not their fault they don't get it right. It's all the awful tutorials and books that people with no understanding of OOP share with the world.

 

Couldn't agree more. For several years I haven't been able to grasp the purpose behind OOP, just because all tutorials insisted on creating 'animal' class that was extended to 'dog' and 'cat' classes...

Link to comment
Share on other sites

Since you are just getting started I'd like to suggest using Autoloading Objects http://us.php.net/autoload .  I wish someone had told me about it when I was first learning.  I started by using a global include file that included ever class file.  Using _autoload will prevent messes includes in your applications.  Learning how to use this was a real highlight for me!

Link to comment
Share on other sites

Couldn't agree more. For several years I haven't been able to grasp the purpose behind OOP, just because all tutorials insisted on creating 'animal' class that was extended to 'dog' and 'cat' classes...

 

I never fully understood OOP until I got hired on by a private guy and coded, school teaches you nothing about proper usage. The hard thing was, that was coding in C# and then trying to go to PHP and mimic behavior was frustrating cause PHP 4 sucked at OOP. PHP 5 was a huge breath of fresh air though!

 

EDIT:

Just a side note, I still have problems with the proper methodology of OOP, it is a tough concept to grasp. Best way is to write an outline of your plan I have found, at least that helps me.

Link to comment
Share on other sites

I never fully understood OOP until I got hired on by a private guy and coded, school teaches you nothing about proper usage.

 

The university I went to taught me how to use OOP very well.  I think that actual work will teach you much more but school has certainly taught me the core concepts.

 

The best way to learn is to use an actual OOP language, Java would be a good choice.

 

Since you are just getting started I'd like to suggest using Autoloading Objects http://us.php.net/autoload .  I wish someone had told me about it when I was first learning.  I started by using a global include file that included ever class file.  Using _autoload will prevent messes includes in your applications.  Learning how to use this was a real highlight for me!

 

Very very useful  ;D

Link to comment
Share on other sites

Yea I went to school for Multimedia but they didn't stress to much programming and/or require programming minus basic C++ and some Javascript / PHP / etc....but no OOP.

 

Yea mchl I agree a lot of the examples I have found are class Animal / class Dog extends Animal etc... I do the same as premiso for any programming create an outline of general functionality of what will be needed.

 

Would autoload be better use in a framework? I have been going over some autoload examples this morning but atm it seems better to just include the file myself.

Link to comment
Share on other sites

Would autoload be better use in a framework? I have been going over some autoload examples this morning but atm it seems better to just include the file myself.

 

Yes, depending how how big your web application is it could be easier.  The easiest would be to include it on a page that is included on every page, such as a header.  That way you never have to include files or autoload on every page.

Link to comment
Share on other sites

Yea I could see that being a great way for that.

 

What if other developers are working on the same project want to use your __autoload func that you wrote up to include them?

Say I write my files class.animal.php and the other guy writes it animal.class.php and another guy writes is just animal.php Or should i just be like this is the way we name files? Any suggestions?

 

<?php

   function __autoload($className) {
        // granted to incorporate different ways to save files I will need to write more code
       
	$file = 'classes/class.'.$className.'.php';
	include_once($file);
}

$test = new testAutoload();	// w/ autoload func it will pull file "classes/class.testAutoload.php"

?>

Link to comment
Share on other sites

Yea I could see that being a great way for that.

 

What if other developers are working on the same project want to use your __autoload func that you wrote up to include them?

Say I write my files class.animal.php and the other guy writes it animal.class.php and another guy writes is just animal.php Or should i just be like this is the way we name files? Any suggestions?

 

<?php

   function __autoload($className) {
        // granted to incorporate different ways to save files I will need to write more code
       
	$file = 'classes/class.'.$className.'.php';
	include_once($file);
}

$test = new testAutoload();	// w/ autoload func it will pull file "classes/class.testAutoload.php"

?>

 

Always have guidlines and rules for naming structure. Keep it consistent it makes it a ton easier in the long haul. If they are not doing that tell them they need to.

 

My preference is name.cls.php if you have a classes folder. If not then I do cls.name.php but either way make that rule and have everyone obey it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.