By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

classes... why?

P: n/a
why would i use a class in php, and how do i use a class in php?
Jul 16 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Tue, 12 Aug 2003 17:34:19 +1000, Sticks wrote:
why would i use a class in php, and how do i use a class in php?


Simplistic: Classes make it easier to write "reusable" code

For more information about classes in php... read the manual (there are some
little examples on how to use them)
also search google for OO and php or OOPS and php (Object Oriented)
Jul 16 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Tue, 12 Aug 2003 10:32:40 +0200, Warstar
<wa*****@NSA.com> amazingly managed to produce the following with
their Etch-A-Sketch:
why would i use a class in php, and how do i use a class in php?

because it is great u can get more readable code and better
security.

How does using classes make things more secure than anything else?

<?php
class foo {
function bar($string) {
system($string);
return;
}
}

foo::bar('rm -rf /');
?>
How is the above more secure than:
system('rm -rf /');

Regards,

Ian

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPzjHzWfqtj251CDhEQJeGQCgqBGRRnfD4icxIYt+BBkoG+ UJhAoAoMeV
F+boGL3TLNoZ/hv7STPhN9Bb
=/QcB
-----END PGP SIGNATURE-----

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
Jul 16 '05 #3

P: n/a
With total disregard for any kind of safety measures "Sticks"
<st*****@hotmail.com> leapt forth and uttered:
why would i use a class in php, and how do i use a class in php?


Read this: http://sitepointforums.com/showthrea...threadid=59898

--
There is no signature.....
Jul 16 '05 #4

P: n/a
Ian.H [dS] wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Tue, 12 Aug 2003 10:32:40 +0200, Warstar
<wa*****@NSA.com> amazingly managed to produce the following with
their Etch-A-Sketch:

why would i use a class in php, and how do i use a class in php?


because it is great u can get more readable code and better
security.


How does using classes make things more secure than anything else?

<?php
class foo {
function bar($string) {
system($string);
return;
}
}

foo::bar('rm -rf /');
?>
How is the above more secure than:
system('rm -rf /');


Because you would probably write checking code inside the class to stop
the user from making stupid mistakes like that.

Jul 16 '05 #5

P: n/a
But that's no different from:

$string = 'rm -rf /';
if ($string != 'rm -rf /') {
system($string);
}
is it?

My point was just that whatever code you use, beit classes, functions
or "inline", it's all as secure as you make it, there is no real
security advantages / disadvantages of using / not using classes if
you code correctly from the outset.. but if you feel that because
code is written "inline" it doesn't need checking / validating.. only
when within a class, then the mind boggles.


Methinks my answer was a little short and didn't explain my meaning
properly. In the case of you, Ian, I realise this is a grandmother-egg
situation, but I'll include a full explanation for the original poster.

If I have an application which deals with something like purchase orders
I would have a database table to hold them. At this point I can write my
php scripts to create, modify and write them. If I only need a couple of
short scripts then I would probably include all my error checking in
these scripts.

However, if the application starts to grow larger the amount of checking
performed in each script starts to become unwieldy. There's also a
danger that I might alter the underlying structure of a purchase order
and fail to update the error checking in every script in the
application. This can easily lead to damaged records in the database.
The situation is even worse if someone else has written custom scripts
which I know nothing about.

At this point all handling of purchase orders should be encapsulated
within a class (or library of functions) which contain a definitive set
of error and consistency checks for dealing with a purchase order. If I
alter the structure or rules for a purchase order I change one class
library to reflect this. Incorrectly written scripts will then fail
(hopefully gracefully) without damaging records in the database.

This also helps to protect me from any odd implementation gotchas which
I've forgotten about in the intervening years since I wrote the
application. Writing programs is one thing, maintaining them over a long
period of time (some over 10 years now) is a completely different game.
Anything which helps to clarify your original thinking or stops you
shooting yourself in the foot is of prime importance.


Jul 16 '05 #6

P: n/a
On Tue, 12 Aug 2003 17:34:19 +1000, "Sticks" <st*****@hotmail.com>
wrote:
why would i use a class in php, and how do i use a class in php?


This is probably going to open a Pandora's Box, but after much playing
around with PHP I have ended up doing procedural program design rather
than object oriented.

Granted, for complex applications it's going to be handy, but because
PHP has no application level objects, setting any complex data
structures and so on up is a bit of a nightmare because you have to do
it at the start of every page - hardly ideal...

Jul 16 '05 #7

P: n/a
No application level objects? What about SRM?
http://www.vl-srm.net/index.php

kafooey wrote:
On Tue, 12 Aug 2003 17:34:19 +1000, "Sticks" <st*****@hotmail.com>
wrote:

why would i use a class in php, and how do i use a class in php?

This is probably going to open a Pandora's Box, but after much playing
around with PHP I have ended up doing procedural program design rather
than object oriented.

Granted, for complex applications it's going to be handy, but because
PHP has no application level objects, setting any complex data
structures and so on up is a bit of a nightmare because you have to do
it at the start of every page - hardly ideal...


Jul 16 '05 #8

P: n/a
"sotto" <ju**@sotto.be> wrote in message
news:pa****************************@sotto.be...
On Tue, 12 Aug 2003 17:34:19 +1000, Sticks wrote:
why would i use a class in php, and how do i use a class in php?
Simplistic: Classes make it easier to write "reusable" code

For more information about classes in php... read the manual (there are

some little examples on how to use them)
also search google for OO and php or OOPS and php (Object Oriented)


Does PHP support inheritance and the like?

I too never understood the point of classes but due to HAVING to learn about
C# which is entirely OO, I've found them to be VERY usefull. I'll certainly
be porting my new found OOP knowledge to my PHP work.

BTW - my advice to anybody who doesn't know what classes are for; go and
learn now! The concept can be quite difficult to grasp, but once you have,
you'll wish you learnt earlier.

Regards,

Nathan
Jul 16 '05 #9

P: n/a
With total disregard for any kind of safety measures
"treefroginometry" <in**@skunkbag.antispam.co.uk> leapt forth and
uttered:
Does PHP support inheritance and the like?

PHP supports inheritence yes. It's missing a number of structures
in PHP4 such as abstract classes, interfaces, private/protected
methods and properties and the like though. These will all be part
of PHP5 however.
I too never understood the point of classes but due to HAVING to
learn about C# which is entirely OO, I've found them to be VERY
usefull. I'll certainly be porting my new found OOP knowledge to
my PHP work.

It's a state of mind that takes a good while to establish. My
housemate is a PHP beginner and I had to walk him through the usage
of basic functions as he hadn't even grasped that fundamental
concept. OOP takes it to a whole new level.
BTW - my advice to anybody who doesn't know what classes are
for; go and learn now! The concept can be quite difficult to
grasp, but once you have, you'll wish you learnt earlier.


Very true.

--
There is no signature.....
Jul 16 '05 #10

P: n/a
In article <Xn*************************@216.196.97.132>,
Phil Roberts <ph*****@HOLYflatnetSHIT.net> wrote:
With total disregard for any kind of safety measures
"treefroginometry" <in**@skunkbag.antispam.co.uk> leapt forth and
uttered:
Does PHP support inheritance and the like?


PHP supports inheritence yes.


True. Multiple inheritance is not, however, supported.

--
CC
Jul 16 '05 #11

P: n/a
treefroginometry wrote:
Does PHP support inheritance and the like?

http://www.php.net/manual/en/language.oop.php


Brian Rodenborn
Jul 16 '05 #12

P: n/a
"sotto" <ju**@sotto.be> wrote in message news:<pa****************************@sotto.be>...
On Tue, 12 Aug 2003 17:34:19 +1000, Sticks wrote:
why would i use a class in php, and how do i use a class in php?


Simplistic: Classes make it easier to write "reusable" code


Net necessarily. I have been writing large chunks of reusable code for
over 20 years without touching OO.

One problem with OO is that developers can start using OO functions
just because they are there instead of the fact that they solve a
given problem. One train of thought goes like this:
1) Re-usable code is a *good* thing.
2) In OOP you can re-use code through inheritance.
3) I can achieve inheritance through subclassing.
4) Therefore I must have as many subclasses as possible.

One of the performance problems with this is that whenever you
instantiate a subclass its parent class must also be instantiated, and
its parent all the way up to the top of the tree. It just goes to
prove that it is possible to write crap code using OOP methods as it
is with non-OOP methods.

If you want to see an example of a date validation class take a look
at http://www.tonymarston.net/php-mysql/dateclass.html.

If you want to see an example of an abstract database class take a
look at http://www.tonymarston.net/php-mysql...seobjects.html and
http://www.tonymarston.net/php-mysql...eobjects2.html

Tony Marston
http"//www.tonymarston.net/
Jul 16 '05 #13

P: n/a
treefroginometry wrote:
"sotto" <ju**@sotto.be> wrote in message
news:pa****************************@sotto.be...
On Tue, 12 Aug 2003 17:34:19 +1000, Sticks wrote:

why would i use a class in php, and how do i use a class in php?


Simplistic: Classes make it easier to write "reusable" code

For more information about classes in php... read the manual (there are


some
little examples on how to use them)
also search google for OO and php or OOPS and php (Object Oriented)

Does PHP support inheritance and the like?

I too never understood the point of classes but due to HAVING to learn about
C# which is entirely OO, I've found them to be VERY usefull. I'll certainly
be porting my new found OOP knowledge to my PHP work.

BTW - my advice to anybody who doesn't know what classes are for; go and
learn now! The concept can be quite difficult to grasp, but once you have,
you'll wish you learnt earlier.

Regards,

Nathan

My first language was C, back in 1987. My second was C++ in 1989(I
think). I never really understood what made OOP so "special" until I
took a course in programming windows with the MFC's. Our first 3
programs were with straight C using the native win32 api. Our next 3
programs were the first 3 using the MFC's. WOW! That's when I truely
learned to appreciate OOP programming. When you can take 14 pages of
code and reduce it to less than 4, that's cool.

John

Jul 16 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.