tags:

views:

177

answers:

4

I know this is very newbyish but I have been using php.net for refernce for at least 2 years now to look up the built in functions and I am just noticing that there is some built in classes as well like here for an example http://www.php.net/manual/en/datetime.settimezone.php I am now remembering seeing some functions in the past that would show an example in procedural way and in class way as well, of course when I wanted to find one of these for an example I couldn't though.

The question here is has these classes been there on php.net for a long time? Also is there any benefit of using one of the built in classes over a built in function?

+1  A: 

Hi,

is there any benefit of using one of the built in classes over a built in function?

It all depends on how you did the conception and developpement of your application :

  • if your application is based on Object-Oriented Programming, it makes sense to use the OO-interface (ie, classes)
  • if your application is based on procedural programming, it probably makes more sense to use the procedural-interface (ie, functions)

In any case, of course, you can choose one API or the other, as you see fit -- the two points I wrote are no more than "logical guidelines", I'd say.


About that :

has these classes been there on php.net for a long time?

Well, I'm guessing, for classes that belong to extensions bundled with PHP (like mysqli, for instance), that they are there since the extension they document exists.

In the case of mysqli, it means PHP 5, it seems (see, for instance, the PHP's version number on top of this page) -- so, I'd say since something like 2005 (I didn't check the date, but it's something like that)


As a sidenote, you have to take care about the fact that php.net hosts documentation for :

  • functions / classes that are always bundled with PHP -- or, at least, almost never de-activated ; for instance, session
  • functions / classes that are exposed by extensions that often come from PHP, and are hosted on the same SVN as PHP's sources... But not necessarily always compiled or actived -- for instance, mysqli or soap (I've worked on a server some time ago where I've had to compile those myself)
  • functions / classes that are exposed by extensions that are not even hosted on the SVN containing PHP's source-code (or, in another directory, different from PHP's one) -- many of those extensions are available as PECL packages -- for instance : APC and it's documentation


Hope this helps make things more clear :-)

Pascal MARTIN
A: 

You're asking if the documentation for those classes have been there for a long time? They've most likely been there since the classes themselves existed.

There's no simple answer to "is class X better than function Y". You need to read the documentation, and see which one works better for you.

Arms
As far as I can tell, most of the functions return the same result as the classes do, that's why it is rather confusing
jasondavis
A: 

Object-oriented functionality was added in PHP 3 and improved in PHP 4. Object handling was completely rewritten for PHP 5.

If you should or not use it, I think it depends on your objectives... If you want to program object oriented, you should use the object oriented interface. If you want procedural, use the functions.

vitorcoliveira
+1  A: 

The question here is has these classes been there on php.net for a long time?

from the page (PHP 5 >= 5.2.0)

Also is there any benefit of using one of the built in classes over a built in function?

For the most part all the DateTime object methods still map to the built-in functions which might suffice. However I know that if you need to do certain timezone conversions then they provide functionality that you cannot get in previous versions of PHP. (for instance if you need to compare two times in arbitrary timezones taking into account daylight savings offsets)

@see http://www.php.net/manual/en/migration52.datetime.php

sbeam
thanks that link clears up a lot of my questions
jasondavis