[PHP] Classes and Functions
Hello, Is there a way to see what objects and functions a script loaded/required/used? I could recursively loop through the globals, but if objects were unset, then i may miss some. I could make a 'tracking' object and every time i load/include a file (which contains a class def or a function def) to add that file to the tracking object...but it would be nice if i didn't have to modify my existing code to see which objects and functions a script actually used, or at least, requested and loaded into memory. Thanks in advance, Daniel Kolbo ` -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes and Functions
On Sun, Nov 1, 2009 at 9:50 PM, Daniel Kolbo kolb0...@umn.edu wrote: Hello, Is there a way to see what objects and functions a script loaded/required/used? I don't think it's possible to that in PHP code. I could recursively loop through the globals, but if objects were unset, then i may miss some. I could make a 'tracking' object and every time i load/include a file (which contains a class def or a function def) to add that file to the tracking object...but it would be nice if i didn't have to modify my existing code to see which objects and functions a script actually used, or at least, requested and loaded into memory. maybe what you are looking for is xdebug (http://xdebug.org/). It provide code coverage analysis. Thanks in advance, Daniel Kolbo ` -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing?
Re: [PHP] Classes and Functions
On Sunday 01 November 2009 2:50:55 pm Daniel Kolbo wrote: Hello, Is there a way to see what objects and functions a script loaded/required/used? I could recursively loop through the globals, but if objects were unset, then i may miss some. I could make a 'tracking' object and every time i load/include a file (which contains a class def or a function def) to add that file to the tracking object...but it would be nice if i didn't have to modify my existing code to see which objects and functions a script actually used, or at least, requested and loaded into memory. Thanks in advance, Daniel Kolbo ` Depends what you are trying to do with it, but I suspect these are a good start: http://www.php.net/get_defined_functions http://www.php.net/get_defined_vars http://www.php.net/get_defined_constants http://www.php.net/get_declared_classes http://www.php.net/get_declared_interfaces http://www.php.net/get_included_files -- Larry Garfield la...@garfieldtech.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes and Functions
Robert Cummings wrote: On Thu, 2005-11-03 at 12:15, Jasper Bryant-Greene wrote: On Wed, 2005-11-02 at 19:05 -0500, Unknown Unknown wrote: Oh thanks again and what does RTFM mean? Read The [insert your favourite word beginning with F here] Manual. Fondu?? *licks lips* you missed an 'e' at the end, but given your great info yesterday regarding DOM and namespaces, I'll spare you a RTFD(ictionary) ;-) Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes and Functions
STFW -Original Message- From: Unknown Unknown [mailto:[EMAIL PROTECTED] Sent: 03 November 2005 00:05 To: Jasper Bryant-Greene Cc: php-general@lists.php.net Subject: Re: [PHP] Classes and Functions Oh thanks again and what does RTFM mean? This message has been delivered to the Internet by the Revenue Internet e-mail service * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Classes and Functions and If's (Problems Again)
Hi everyone, i have some code that proccesses a login form. i have an object $DB which works only in SOME areas, this is the code: ?php require_once(Include.php); global $DB; $Username= $_REQUEST['Username']; $Password= $_REQUEST['Password']; $SQL= SELECT * FROM members WHERE Username='$Username' AND Password='$Password'; ; $DB-Query($SQL); $UserInfo=$DB-QueryInfo(Array); if($UserInfo==0) $Login=FALSE; else $Login=TRUE; //Make Sure the user is not a duplicate user trying to log in again if($Login=TRUE) { global $DB; $RL=__; $LoginD=__; $ID=$UserInfo['ID']; echo $ID; $DB=membersp; $SQL=SELECT ID FROM membersonline WHERE ID='$ID' ; $DB-Query($SQL); $CheckFD=$Result; if($CheckFD!=0) $LoginD=TRUE; else $LoginD=FALSE; if($LoginD==TRUE) { $RL=TRUE; //code to allow user onto next page without interfering with the security measures $DB=membersp; $SQL=SELECT * FROM membersonline WHERE ID=$ID; DBConnect($DB,$SQL); $Slide=$Result; $NextCode=$Slide['NextCode']; } if($LoginD==FALSE) { // Now we start to create and write the data that will let other pages know if the user is logged in or not $Seed= rand(); srand($Seed); $NextCode= rand(); $PageNumber=1; $DB=membersp; $SQL=INSERT INTO MembersOnline (ID, Seed, PageNumber, NextCode) VALUES ('{$ID}', '$Seed', '$PageNumber', '$NextCode') ; DBConnect($DB,$SQL); } } ? !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd; html xmlns=http://www.w3.org/1999/xhtml; head ?php Init(2); ? title/title /head body ?php if($Login==FALSE) echo(p class='error'Login failed, wrong username and/or password please a href='Login.php'Try Again/a or a href=' Register.php'Register/a/p); if(@$RL==TRUE) echo(p class='error'You have already logged in, click a href='Projects.php?Code={$NextCode}'Here/a to go on/p); ? /body /html i get an error saying: *Fatal error*: Call to a member function Query() on a non-object in * D:\Apache\Apache(re)\Apache2\htdocs\LoginP.php* on line *21* but before line 21 i use $DB and it works fine, but line 21 is a problem, any help appreciated
Re: [PHP] Classes and Functions and If's (Problems Again)
On Thu, 2005-11-03 at 18:37 -0500, Unknown Unknown wrote: Hi everyone, i have some code that proccesses a login form. i have an object $DB which works only in SOME areas, this is the code: ?php require_once(Include.php); global $DB; There is no need to global $DB here. You are already in the global scope. $Username= $_REQUEST['Username']; $Password= $_REQUEST['Password']; $SQL= SELECT * FROM members WHERE Username='$Username' AND Password='$Password'; ; $DB-Query($SQL); $UserInfo=$DB-QueryInfo(Array); if($UserInfo==0) $Login=FALSE; else $Login=TRUE; Why aren't you using real boolean values here? //Make Sure the user is not a duplicate user trying to log in again if($Login=TRUE) { global $DB; Again, no need to global $DB. You are still in the global scope. $RL=__; $LoginD=__; $ID=$UserInfo['ID']; echo $ID; $DB=membersp; Here is your problem. You just replaced the $DB object with a string. Maybe you meant to set a property on the $DB object? I think you may need to do some reading on OOP, perhaps start at http://php.net/oop for PHP4 or http://php.net/oop5 for PHP5. -- Jasper Bryant-Greene General Manager Album Limited e: [EMAIL PROTECTED] w: http://www.album.co.nz/ b: http://jbg.name/ p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303 a: PO Box 579, Christchurch 8015, New Zealand -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes and Functions and If's (Problems Again)
On 11/4/05, Unknown Unknown [EMAIL PROTECTED] wrote: Hi everyone, i have some code that proccesses a login form. i have an object $DB which works only in SOME areas, this is the code: [...] $DB=membersp; $SQL=SELECT ID FROM membersonline WHERE ID='$ID' ; $DB-Query($SQL); [...] i get an error saying: *Fatal error*: Call to a member function Query() on a non-object in * D:\Apache\Apache(re)\Apache2\htdocs\LoginP.php* on line *21* but before line 21 i use $DB and it works fine, but line 21 is a problem, any help appreciated You've not indicated which is line 21, but I presume it's the bit shortly after you assigned a string to $DB. A string is not an object, hence the error message. -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Classes and Functions
Hi everybody i have a class that i reference with: $DB= new DBInterface; outside a function it works fine, but using $DB inside a function changes the data type i think... i get an error saying i'm using a method on a non-object... any help appreciatted
Re: [PHP] Classes and Functions
On Wed, 2005-11-02 at 18:56 -0500, Unknown Unknown wrote: Hi everybody i have a class that i reference with: $DB= new DBInterface; outside a function it works fine, but using $DB inside a function changes the data type i think... i get an error saying i'm using a method on a non-object... any help appreciatted If you are attempting to access the variable $DB inside the function scope, like this: $DB = new DBInterface; function doSomething() { $DB-doSomethingElse(); } Then it will not work unless you either pass $DB to doSomething(), or declare $DB as a global at the start of the function. Like this: function doSomething() { global $DB; $DB-doSomethingElse(); } Or access $DB through the globals superglobal. RTFM. -- Jasper Bryant-Greene General Manager Album Limited e: [EMAIL PROTECTED] w: http://www.album.co.nz/ b: http://jbg.name/ p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303 a: PO Box 579, Christchurch 8015, New Zealand -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes and Functions
On 11/2/05, Unknown Unknown [EMAIL PROTECTED] wrote: Hi everybody i have a class that i reference with: $DB= new DBInterface; outside a function it works fine, but using $DB inside a function changes the data type i think... i get an error saying i'm using a method on a non-object... any help appreciatted if you mean by inside a function, a method of your class DBInterface then you can can use $this. Otherwise you can do something like: $DB= new DBInterface(); and then I believe you should be able to use $DB at it is.
Re: [PHP] Classes and Functions
A thanks, i tried that before didn't seem to work, now it works Thanks again!
Re: [PHP] Classes and Functions
Oh thanks again and what does RTFM mean?
Re: [PHP] Classes and Functions
On Wed, 2005-11-02 at 19:05 -0500, Unknown Unknown wrote: Oh thanks again and what does RTFM mean? Read The [insert your favourite word beginning with F here] Manual. -- Jasper Bryant-Greene General Manager Album Limited e: [EMAIL PROTECTED] w: http://www.album.co.nz/ b: http://jbg.name/ p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303 a: PO Box 579, Christchurch 8015, New Zealand -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes and Functions
On Thu, 2005-11-03 at 12:15, Jasper Bryant-Greene wrote: On Wed, 2005-11-02 at 19:05 -0500, Unknown Unknown wrote: Oh thanks again and what does RTFM mean? Read The [insert your favourite word beginning with F here] Manual. Fondu?? *licks lips* Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Classes or functions
What this methods are better for php4? classes or functions Does it better to use classes whennbsp;I can write the codes with functions? Excuseme for my bad english
[PHP] Classes or functions
What this methods are better for php4? classes or functions Does it better to use classes when I can write the codes with functions? Excuseme for my bad english
Re: [PHP] Classes or functions
It's really a matter of comfort level and opinion. There can be strong arguments either way. Using just functions will be faster, although the speed difference may be minimal and not noticeable. Using just functions can be easier and quicker to develop, especially for small projects. But, using classes will allow your project to scale much better, if only by giving you the ability to create variable scopes so that variable and function names don't conflict. Classes will also allow you to standardize your function names. For instance, you can have multiple functions called getList(), but in different classes. Then any time you need to retrieve a list of data, you call the getList() function from the appropriate class (i.e. companies, contacts, phones, etc). That's a very basic example. My personal opinion is that you cannot build a large scale project that will be easily maintainable without using classes and object oriented concepts. That's not to say it can't or hasn't been done (it has). On Oct 19, 2005, at 12:42 PM, Khorosh Irani wrote: What this methods are better for php4? classes or functions Does it better to use classes when I can write the codes with functions? Excuseme for my bad english -- Brent Baisley Systems Architect Landover Associates, Inc. Search Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes and Functions
your question is hard to answer. The difference between classes and functions is like the difference between a toolbox, and a spanner. A class is a container, that contains functions, and variables, or as they are called in Object-Oriented-Speak(methods, and properties). A method is a function that exists in a class, although in php they are still declared as functions. function do_something(){ echo Im doing something this is a function; } class class_thing{ function method_of_thing(){ echo I am a method(function) that exists inside of class class_thing; } } Hope this helps, but from what I'm reading, you really need to learn some basics before you even think about classes. Worry about functions for now. Read the PHP manual(you don't get better docs, than the php ones). It explains things like that in a easy to use manner. I also don't know of a better programming tutorial than the PHP manual. Good luck Rory On Sat, 11 Dec 2004 10:23:12 -0800, Robby Russell [EMAIL PROTECTED] wrote: On Sat, 2004-12-11 at 13:03 -0500, R. Van Tassel wrote: Can someone give me a distinction between the two and when to use / not use them? I want to thank everyone who replied about myFor loop question. All the answers were VERY helpful! Thanks very much. You might want to pick up a book on programming to read up on this. In a nutshell, a function should do something specific. It's good to use functions when you do the same thing in different places in your code so that you don't need to copy/paste your code over and over. function foo() { $x = 1; $y = 2; return $x + $y; } $bar = foo(); print $bar; - OUTPUT: 3 A class/object is a collection of functions and variables that are contained within their own scope. (vague description) class foobar { var $x = 10; var $y = NULL; function foobar($y) { $this-y = $y; } function add() { return $this-x + $this-y; } function subtract() { return $this-x - $this-y; } } $object = new foobar(4); print $object-add() . \n; print $object-subtract() . \n; # set y to a new number $object-y = 2; print $object-add() . \n; print $object-subtract() . \n; OUTPUT: 14 6 12 8 As it sounds like you're still new to this, I would pick up a book or read some material online that will better show you when to use either and play around with them both. Have fun -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting Development *--- Now supporting PHP5 --- / -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Classes and Functions
Can someone give me a distinction between the two and when to use / not use them? I want to thank everyone who replied about myFor loop question. All the answers were VERY helpful! Thanks very much. ~ R. Van Tassel
Re: [PHP] Classes and Functions
On Sat, 2004-12-11 at 13:03 -0500, R. Van Tassel wrote: Can someone give me a distinction between the two and when to use / not use them? I want to thank everyone who replied about myFor loop question. All the answers were VERY helpful! Thanks very much. You might want to pick up a book on programming to read up on this. In a nutshell, a function should do something specific. It's good to use functions when you do the same thing in different places in your code so that you don't need to copy/paste your code over and over. function foo() { $x = 1; $y = 2; return $x + $y; } $bar = foo(); print $bar; - OUTPUT: 3 A class/object is a collection of functions and variables that are contained within their own scope. (vague description) class foobar { var $x = 10; var $y = NULL; function foobar($y) { $this-y = $y; } function add() { return $this-x + $this-y; } function subtract() { return $this-x - $this-y; } } $object = new foobar(4); print $object-add() . \n; print $object-subtract() . \n; # set y to a new number $object-y = 2; print $object-add() . \n; print $object-subtract() . \n; OUTPUT: 14 6 12 8 As it sounds like you're still new to this, I would pick up a book or read some material online that will better show you when to use either and play around with them both. Have fun -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting Development *--- Now supporting PHP5 --- / -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Hi, am Wednesday 23 July 2003 11:48 schrieb Joel Rees: (You know that $accesses-count and $accesses-resetCounter() are at least declared in the same class declaration. $accesses_count and accesses_resetCounter() could be declared in entirely unrelated include files for entirely different purposes.) That is the only benefit. But it's not OOP. If you need namespaces and can't take care of them w/o misuse of a semi-OOP construct youre not any more efficient with it. misuse? (I leaned sideways and cross my eyes, then I stood on my head, but that still makes no sense.) Is there any performance benefit one way or the other? Programmer performance? I used EZ_Sql which is cool but didn't seem to speed things up in comparison to the said include file. Still don't see the beef. Execution speed isn't all that matters. In fact, speed is not the point at all. Then you must be an PHPNuke or Typo3-programmer, beeing lucky to get at least 1 request per second ;). Oh, yeah, gotta watch those bees. Don't take it hard, but If you had ever been in computer-science [school|college|...] you would know that speed is all that matters. Sure. ROFDDCI -- Joel Rees, programmer, Kansai Systems Group Altech Corporation (Alpsgiken), Osaka, Japan http://www.alpsgiken.co.jp Rate that troll, maybe a 4/10? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Say I have a database class with all kinds of functions for connection, data manipulation, errors, etc: $db = new db_sql; $db-connect(); $db-do_this(); $db-do_that(); How would that be different from an include file with a bunch of functions doing the same thing? As far as I'm concerned, the primary benefit of classes is that you don't clutter up your namespace. If you have, for instance, $accesses-count, you know more about that count than if you have just $count. It's a method of organizing your code that doesn't (once you get used to it) get in the way. Also, you can be really sure that $accesses-resetCounter() is somehow related to $accesses-count, whereas $accesses_count and accesses_resetCounter() might win your confidence incorrectly. Or maybe you slip and use access_resetCounter() instead, which might be the cause of no end of grief. (You know that $accesses-count and $accesses-resetCounter() are at least declared in the same class declaration. $accesses_count and accesses_resetCounter() could be declared in entirely unrelated include files for entirely different purposes.) Is there any performance benefit one way or the other? Programmer performance? I used EZ_Sql which is cool but didn't seem to speed things up in comparison to the said include file. Still don't see the beef. Execution speed isn't all that matters. In fact, speed is not the point at all. Brad recommended I spend even more time with google looking for OOP, maybe that's why I'm so confused:). I think he's trying to point you to the mind-set. Do you like complex puzzles with intricate interactions where you have to keep tens of thousands of lines of code constantly in your head? Or do you like to focus on several hundred lines at a time, get those to behave, then move on to another part with a fairly high level of confidence that you won't shoot yourself in the foot by using the wrong count or resetCount() somewhere? -- Joel Rees, programmer, Kansai Systems Group Altech Corporation (Alpsgiken), Osaka, Japan http://www.alpsgiken.co.jp -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Hi, am Wednesday 23 July 2003 11:48 schrieb Joel Rees: (You know that $accesses-count and $accesses-resetCounter() are at least declared in the same class declaration. $accesses_count and accesses_resetCounter() could be declared in entirely unrelated include files for entirely different purposes.) That is the only benefit. But it's not OOP. If you need namespaces and can't take care of them w/o misuse of a semi-OOP construct youre not any more efficient with it. Is there any performance benefit one way or the other? Programmer performance? I used EZ_Sql which is cool but didn't seem to speed things up in comparison to the said include file. Still don't see the beef. Execution speed isn't all that matters. In fact, speed is not the point at all. Then you must be an PHPNuke or Typo3-programmer, beeing lucky to get at least 1 request per second ;). Don't take it hard, but If you had ever been in computer-science [school|college|...] you would know that speed is all that matters. bg Sam -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Execution speed isn't all that matters. In fact, speed is not the point at all. Then you must be an PHPNuke or Typo3-programmer, beeing lucky to get at least 1 request per second ;). Don't take it hard, but If you had ever been in computer-science [school|college|...] you would know that speed is all that matters. troll. :) Execution speed is most definitely /not/ all that matters, though it really depends on the situation. It is often cheaper to throw CPU/storage/RAM at a problem than it is to spend more developer time, especially when one of the considerations is future maintenance or upgrades. In regards to education, OOP-techniques and other lisp-isms evolved in academic settings, in response to concerns about code portability, maintenance, and elegance. Academic code frequently sacrifices speed in the service of technique. - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] classes v. functions
Hi, MM troll. :) MM Execution speed is most definitely /not/ all that matters, though it MM really depends on the situation. It is often cheaper to throw MM CPU/storage/RAM at a problem than it is to spend more developer time, MM especially when one of the considerations is future maintenance or MM upgrades. In regards to education, OOP-techniques and other lisp-isms MM evolved in academic settings, in response to concerns about code MM portability, maintenance, and elegance. Academic code frequently MM sacrifices speed in the service of technique. I have just done a test to see how much is lost with using classes by changing a template class I use back to functions and globals. Over 10 iterations of a template file with 100 blocks the average speedup per iteration was 1.8ms on a total of 19ms (the class version) So the gains made by using a class structure far outway any loss of speed in my opinion. -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Hi, Saturday, July 19, 2003, 7:08:55 AM, you wrote: A This may show my ignorance or my refusal to take for granted something I A don't fully understand but I have a hard time figuring out the advantage of A using classes as opposed to just functions. I am certainly new to php and A at first sight classes seemed to cut a lot of corners but so do functions A (with which I have more experience). The more I read about classes the A deeper the confusion. Anyone can enlighten me? A Regards, Andu Novac Classes are good in that they group functions and variables into a re usable block. Trying to do everything by just loading functions will eventually lead to naming and include hell once you start doing complex things. PHP might not be an oo language but it sure helps me keep things togeher. I tend to make classes that do a function like authentication, I just need to include authClass.inc and fire it up and all my auth work is done. i know I can create other functions as they won't clash with the ones in the auth class. Another great thing with classes is you don't have to make common variables global all the time. If you haven't used any other type of OOP then you won't have any problems trying to make PHP look like java, you can just grab the benifits with whats available :-) -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Thanks to all for your input. Regards, Andu Novac -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Whether it's the right move or not, I've put off my learning of PHP classes and OOP methods until PHP5.x, so I guess the time in near :) However, *especially* if you're the only programmer on a project, you can take the ideas benefits behind OOP (for me, having clear, clean code that can be reused over and over again with minimal changes is my primary goal) and apply it to your own code. It takes strict naming conventions and some careful planning, but I don't see that as a limitation at all -- more like a benefit :) Justin On Saturday, July 19, 2003, at 07:08 AM, Andu wrote: This may show my ignorance or my refusal to take for granted something I don't fully understand but I have a hard time figuring out the advantage of using classes as opposed to just functions. I am certainly new to php and at first sight classes seemed to cut a lot of corners but so do functions (with which I have more experience). The more I read about classes the deeper the confusion. Anyone can enlighten me? Regards, Andu Novac -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- [This E-mail scanned for viruses] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] classes v. functions
This may show my ignorance or my refusal to take for granted something I don't fully understand but I have a hard time figuring out the advantage of using classes as opposed to just functions. I am certainly new to php and at first sight classes seemed to cut a lot of corners but so do functions (with which I have more experience). The more I read about classes the deeper the confusion. Anyone can enlighten me? Regards, Andu Novac -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
--- Andu [EMAIL PROTECTED] wrote: This may show my ignorance or my refusal to take for granted something I don't fully understand but I have a hard time figuring out the advantage of using classes as opposed to just functions. They are entirely different. While OO fans will find my over-simplification disgusting, a class is basically a way to associate data with the functions that manipulate that data. Chris = Become a better Web developer with the HTTP Developer's Handbook http://httphandbook.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Chris Shiflett wrote: --- Andu [EMAIL PROTECTED] wrote: This may show my ignorance or my refusal to take for granted something I don't fully understand but I have a hard time figuring out the advantage of using classes as opposed to just functions. They are entirely different. While OO fans will find my over-simplification disgusting, a class is basically a way to associate data with the functions that manipulate that data. Chris AKA 'encapsulation' Of course you can achieve this by making a programmer imposed requirement saying that global variable X can only be altered by function Xf(), or Typedef X can only be altered by functions Xf(), but OO make this more of a hard requirement, and not the programmer imposed or 'soft' requirement. Of course there are other ideas like polymorphism and inheritance and such. Just google for Object oriented programming, and read some of the numerous exposes on the subject to get a general idea. http://www.google.com/search?hl=enie=UTF-8oe=UTF-8q=object+oriented+programming -Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
--On Friday, July 18, 2003 14:28:02 -0700 Chris Shiflett [EMAIL PROTECTED] wrote: --- Andu [EMAIL PROTECTED] wrote: This may show my ignorance or my refusal to take for granted something I don't fully understand but I have a hard time figuring out the advantage of using classes as opposed to just functions. They are entirely different. While OO fans will find my over-simplification disgusting, a class is basically a way to associate data with the functions that manipulate that data. Say I have a database class with all kinds of functions for connection, data manipulation, errors, etc: $db = new db_sql; $db-connect(); $db-do_this(); $db-do_that(); How would that be different from an include file with a bunch of functions doing the same thing? Is there any performance benefit one way or the other? I used EZ_Sql which is cool but didn't seem to speed things up in comparison to the said include file. Still don't see the beef. Brad recommended I spend even more time with google looking for OOP, maybe that's why I'm so confused:). Chris Regards, Andu Novac -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
On Fri, 2003-07-18 at 18:19, Andu wrote: Say I have a database class with all kinds of functions for connection, data manipulation, errors, etc: $db = new db_sql; $db-connect(); $db-do_this(); $db-do_that(); How would that be different from an include file with a bunch of functions doing the same thing? Is there any performance benefit one way or the other? I used EZ_Sql which is cool but didn't seem to speed things up in comparison to the said include file. Still don't see the beef. Using your above example, the above would keep all of the internal data that keeps track of the resource ID (for the connection), the current result set, and the current row, etc. within the object instance scope. Thus everytime you invoke a method on the object it works with it's own set of data. In all honesty you can do this with functions also, but then you would have something like the following: $dbId = db_connect(); $data = db_do_this( $dbId ); $moreData = db_do_that( $dbId, $data ) As you can see this quickly can become cumbersome becaus eof the lack of encapsulation. However, the OOP paradigm goes even further. Lets imagine you now want log all use of your functions (methods :). To do this in OOP you just extend the class and override the methods while still allowing invocation of the inheritted methods. Thus your usage of your new object code would look something like follows: $db = new custom_db_sql; $db-connect(); $db-do_this(); $db-do_that(); Whereas the function code would resemble the following: $dbId = custom_db_connect(); $data = custom_db_do_this( $dbId ); $moreData = custom_db_do_that( $dbId, $data ) As you can see updating the function based code required more work. It wasn't as modular since it couldn't take advantage of inheritance. This is a trivial example btw, it's not perfect since there are books and books written on this topic and I'm obviously glossing over a lot of details. Once you get into design patterns you'll also see other advantages of using objects versus functions. I hope this helps a teeny weeny bit. Cheers, Rob. -- .-. | Worlds of Carnage - http://www.wocmud.org | :-: | Come visit a world of myth and legend where | | fantastical creatures come to life and the | | stuff of nightmares grasp for your soul.| `-' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes v. functions
Andu [EMAIL PROTECTED] wrote: --On Friday, July 18, 2003 14:28:02 -0700 Chris Shiflett [EMAIL PROTECTED] wrote: --- Andu [EMAIL PROTECTED] wrote: This may show my ignorance or my refusal to take for granted something I don't fully understand but I have a hard time figuring out the advantage of using classes as opposed to just functions. They are entirely different. While OO fans will find my over-simplification disgusting, a class is basically a way to associate data with the functions that manipulate that data. Say I have a database class with all kinds of functions for connection, data manipulation, errors, etc: $db = new db_sql; $db-connect(); $db-do_this(); $db-do_that(); How would that be different from an include file with a bunch of functions doing the same thing? Is there any performance benefit one way or the other? classes are slower than functions. Remember php was not and is not (will not be?) designed for OO programming. its designed for web programming. now tools like a class::$db may be slower (like anyone will notice a difference cept in high crunch situations) it makes a lot easier for fexibilty. ie.. changing database servers down the line.. Curt -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Classes vs. functions?
I have been mostly using function in my php code, and lately I have been curious about classes. I have a vague idea of classes from my C++ class that I took a few years back. Can someone please explain to me when is a class useful over a set of functions? I have seen very few code with classes, so pretty much always stayed away from it. Thanks, Vijay Avarachen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. functions?
Can someone please explain to me when is a class useful over a set of functions? Almost always. Object oriented programming offers many many advantages (that I cannot get into depth on here). There are a few exceptions where you would not use classes, such as utility functions like max() and min() that would be used generically. The most basic purpose of a class is to define your own 'type' of object. When you create a class you are encapsulating an 'idea' into a set of attributes and behaviors specific to that type. Designing classes not only offers a great way of organizing code but inevitably forces you to think thoroughly about the logical separation of code and to refine more and more towards an idealized type with well defined and sensible behavior. Conversely non-object oriented programming tends to lack in clarity and intended usage, making it difficult and confusing to use, getting exponentially worse for complex problems. Classes have methods (functions) that are designed to work specifically on members of that class or perform operations directly related to it. Plain functions, however, have a tendency to be vague and complicated by argument lists. Furthermore they are easier to be misused and can be prone to latent errors. In the end, it's like driving a car: the engine is neatly hidden under the hood and you don't care or want to know about the internal workings of it. You just want to get in, turn the key and drive. Classes are no different... they provide a simplified user-friendly interface to complex things. It's luxury in programming :) I would suggest getting some books to learn further, and just start using classes. Steven J. Walker Walker Effects www.walkereffects.com [EMAIL PROTECTED] On Monday, June 2, 2003, at 08:31 PM, Vijay Avarachen wrote: I have been mostly using function in my php code, and lately I have been curious about classes. I have a vague idea of classes from my C++ class that I took a few years back. Can someone please explain to me when is a class useful over a set of functions? I have seen very few code with classes, so pretty much always stayed away from it. Thanks, Vijay Avarachen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. functions?
Here is an excellent article describing classes/objects - what they are and are not. http://phpmag.net/itr/online_artikel/psecom,id,284,nodeid,114.html olinux Can someone please explain to me when is a class useful over a set of functions? __ Do you Yahoo!? Yahoo! Calendar - Free online calendar with sync to Outlook(TM). http://calendar.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] classes and functions in include files
just a fast question here.. and the lotic probably isnt to bright on my part and i think i know the answer to this question too but just to make sure... if you can include variables in an include file and use them outside that file (in the file that includes that file that is) then can you do the same with classes and functions --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.458 / Virus Database: 257 - Release Date: 2/24/2003 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] classes and functions in include files
just a fast question here.. and the lotic probably isnt to bright on my part and i think i know the answer to this question too but just to make sure... if you can include variables in an include file and use them outside that file (in the file that includes that file that is) then can you do the same with classes and functions Yes. Most people put their common functions and classes into a single file that's included() on every other page. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
Thank you for your 2 cents I am just learning and appreciate your comments. - Original Message - From: Michael Hall [EMAIL PROTECTED] To: Chris Crane [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Tuesday, July 16, 2002 11:13 PM Subject: Re: [PHP] Classes vs. Functions There is no simple answer here. I have started using classes where I find I am writing a lot of related functions that share similar parameters. Database connection and queries are a good example. Authentication is another. I have another class that builds forms, because I just hate the tedium of coding HTML forms by hand. It is really just a collection of functions, though, and could work fine as such. I'm still learning/exploring ... I am always guided by the principle that whatever makes less work for me (but achieves the same result) is probably a good thing. IMHO classes are best for more universal code that really can be used in many different places. My functions tend to be more application specific. My 2 cents Michael On Tue, 16 Jul 2002, Chris Crane wrote: Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? Thanks. -- n i n t i . c o m php-python-perl-mysql-postgresql Michael Hall [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Classes vs. Functions
Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? Thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes vs. Functions
[snip] Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? [/snip] A class is the representation of an object, such as a person, place, or thing. A function is a group of commands that can be called for a specific purpose. function addNumbers() A function can be performed on an object, but an object (the logical extension of class) cannot be performed on a function. Does that help? Jay Cleverly disguised as a responsible adult * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes vs. Functions
To add to the below, object classes, can have their own set of functions, called methods. These methods are specific to the function, and ALL functions within a class treat any properties (variables) within the class as global. So if you have a class Car with two properties (Make and Model), then those two properties can be readily accessible to any method within the class. At least I think that's how it works. I'm not all that knowledgeable on the subject :o) Martin Clifford Homepage: http://www.completesource.net Developer's Forums: http://www.completesource.net/forums/ Jay Blanchard [EMAIL PROTECTED] 07/16/02 10:51AM [snip] Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? [/snip] A class is the representation of an object, such as a person, place, or thing. A function is a group of commands that can be called for a specific purpose. function addNumbers() A function can be performed on an object, but an object (the logical extension of class) cannot be performed on a function. Does that help? Jay Cleverly disguised as a responsible adult * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
It helps a little bit, thank you. Could you provide some code as to what a Class looks like. I am just trying to understand it better and if I see it, it might help. Jay Blanchard [EMAIL PROTECTED] wrote in message 000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt... [snip] Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? [/snip] A class is the representation of an object, such as a person, place, or thing. A function is a group of commands that can be called for a specific purpose. function addNumbers() A function can be performed on an object, but an object (the logical extension of class) cannot be performed on a function. Does that help? Jay Cleverly disguised as a responsible adult * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes vs. Functions
[snip] example? [/snip] http://www.devshed.com/Server_Side/PHP/FormValidatorClass/page1.html Good tutorial Jay * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
Here is a sample code (don't take this as gospel... Jay knows more about OOP than I do, I'm sure!): class Car { var $make; var $model; function setMake($x) { $this-make = $x; } function setModel($y) { $this-model = $y; } } $make and $model are the properties of the Car class, and setMake() and setModel() are the methods of the Car class. You don't actually get an object until make a *new* one. Like this: $passat = new Car; $passat-setMake(Volkswagen); $passat-setModel(Passat); This assigns the new object, passat, the properties relating to it's make and model, using the methods setMake and setModel. I'm sure this is right, but I'm sure I'm probably wrong (hehe, confused?). I'm also sure someone will correct me if I am! :o) Martin Clifford Homepage: http://www.completesource.net Developer's Forums: http://www.completesource.net/forums/ Chris Crane [EMAIL PROTECTED] 07/16/02 11:06AM It helps a little bit, thank you. Could you provide some code as to what a Class looks like. I am just trying to understand it better and if I see it, it might help. Jay Blanchard [EMAIL PROTECTED] wrote in message 000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt... [snip] Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? [/snip] A class is the representation of an object, such as a person, place, or thing. A function is a group of commands that can be called for a specific purpose. function addNumbers() A function can be performed on an object, but an object (the logical extension of class) cannot be performed on a function. Does that help? Jay Cleverly disguised as a responsible adult * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
This helps quite a bit Thank you. I am just wondering if I should make classes instead of functions? What would be the benefit of that? Do you know? Martin Clifford [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Here is a sample code (don't take this as gospel... Jay knows more about OOP than I do, I'm sure!): class Car { var $make; var $model; function setMake($x) { $this-make = $x; } function setModel($y) { $this-model = $y; } } $make and $model are the properties of the Car class, and setMake() and setModel() are the methods of the Car class. You don't actually get an object until make a *new* one. Like this: $passat = new Car; $passat-setMake(Volkswagen); $passat-setModel(Passat); This assigns the new object, passat, the properties relating to it's make and model, using the methods setMake and setModel. I'm sure this is right, but I'm sure I'm probably wrong (hehe, confused?). I'm also sure someone will correct me if I am! :o) Martin Clifford Homepage: http://www.completesource.net Developer's Forums: http://www.completesource.net/forums/ Chris Crane [EMAIL PROTECTED] 07/16/02 11:06AM It helps a little bit, thank you. Could you provide some code as to what a Class looks like. I am just trying to understand it better and if I see it, it might help. Jay Blanchard [EMAIL PROTECTED] wrote in message 000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt... [snip] Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? [/snip] A class is the representation of an object, such as a person, place, or thing. A function is a group of commands that can be called for a specific purpose. function addNumbers() A function can be performed on an object, but an object (the logical extension of class) cannot be performed on a function. Does that help? Jay Cleverly disguised as a responsible adult * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes vs. Functions
$this-numberrows; $x++) { $this-result[$x] = mysql_fetch_row($this-qry); } } else { //echo([Error:] Retrieving data); return false; } return true; } } function insertquery() { global $TRUE, $FALSE; if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-qry = mysql_query($this-sql); if (!$this-qry) { return false; } else { return true; } } function deletequery() { global $TRUE, $FALSE; if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-qry = mysql_query($this-sql); if (!$this-qry) { return false; } else { return true; } } function updatequery() { global $TRUE, $FALSE; if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-qry = mysql_query($this-sql); if (!$this-qry) { return false; } else { return true; } } } ? And now in any .php file you include this .obj file and use it as follows. $db1 = new mysqldb(); // create a new instance of the mysql object. // You can create as many as you want and the great thing is // that each instances vars will contain there own data. $sql = Insert into ...; $db1-setTransactionSwitch(true); $db1-begin(); //this will begin a transaction (InnoDb or BDB tables are required for this) $db1-setsql($sql); //set the objects $sql variable. if (!$db1-insertquery()) //now call the method that does all the work so $db1-setTransactionSwitch(false); //you don't have to recode it. If it fails roolback. if($db1-getTransactionSwitch()==false);{ $db1-rollback(); }else{ $db1-commit(); } Classes allow you to have multple instances of an object containing seperate info, makes it easier to keep track of. Hope this helps, Steve. -Original Message- From: Chris Crane [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 16, 2002 11:07 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Classes vs. Functions It helps a little bit, thank you. Could you provide some code as to what a Class looks like. I am just trying to understand it better and if I see it, it might help. Jay Blanchard [EMAIL PROTECTED] wrote in message 000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt... [snip] Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? [/snip] A class is the representation of an object, such as a person, place, or thing. A function is a group of commands that can be called for a specific purpose. function addNumbers() A function can be performed on an object, but an object (the logical extension of class) cannot be performed on a function. Does that help? Jay Cleverly disguised as a responsible adult * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes vs. Functions
[snip] This helps quite a bit Thank you. I am just wondering if I should make classes instead of functions? What would be the benefit of that? Do you know? [/snip] You should keep classes and functions seperate in your thought process, they are not the same and not meant to do or be the same thing. Here is a good thought process to apply to classes... 1. Is this an object? If the answer is no, you do not need a class. 2. Do I need to do something (have an action or set of actions performed), perhaps over and over again? If the answer is yes, this is a function. A method is a function that applies to a specific class of objects. If you created a class and methods for each item you code would be bloated, slow, and hard to maintain. Do not think of functions and classes in the same light, you will make yourself more confused. One is definitely not the other. Jay * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
I'm still trying to figure that out, but the fog is clearing slowly but steadily :o) From what I've heard on this and other lists, it's all a matter of preference. Obviously those that come from an object-oriented environment (Java, etc), will lean toward this method, while others stay with the procedural side of things (using functions). It's all a matter of preference, and just yet I haven't decided which is more useful :o) Good luck! Martin Clifford Homepage: http://www.completesource.net Developer's Forums: http://www.completesource.net/forums/ Chris Crane [EMAIL PROTECTED] 07/16/02 11:19AM This helps quite a bit Thank you. I am just wondering if I should make classes instead of functions? What would be the benefit of that? Do you know? Martin Clifford [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Here is a sample code (don't take this as gospel... Jay knows more about OOP than I do, I'm sure!): class Car { var $make; var $model; function setMake($x) { $this-make = $x; } function setModel($y) { $this-model = $y; } } $make and $model are the properties of the Car class, and setMake() and setModel() are the methods of the Car class. You don't actually get an object until make a *new* one. Like this: $passat = new Car; $passat-setMake(Volkswagen); $passat-setModel(Passat); This assigns the new object, passat, the properties relating to it's make and model, using the methods setMake and setModel. I'm sure this is right, but I'm sure I'm probably wrong (hehe, confused?). I'm also sure someone will correct me if I am! :o) Martin Clifford Homepage: http://www.completesource.net Developer's Forums: http://www.completesource.net/forums/ Chris Crane [EMAIL PROTECTED] 07/16/02 11:06AM It helps a little bit, thank you. Could you provide some code as to what a Class looks like. I am just trying to understand it better and if I see it, it might help. Jay Blanchard [EMAIL PROTECTED] wrote in message 000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt... [snip] Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? [/snip] A class is the representation of an object, such as a person, place, or thing. A function is a group of commands that can be called for a specific purpose. function addNumbers() A function can be performed on an object, but an object (the logical extension of class) cannot be performed on a function. Does that help? Jay Cleverly disguised as a responsible adult * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
thanks Jay. Jay Blanchard [EMAIL PROTECTED] wrote in message 000e01c22cdd$0d0c7530$8102a8c0@niigziuo4ohhdt">news:000e01c22cdd$0d0c7530$8102a8c0@niigziuo4ohhdt... [snip] This helps quite a bit Thank you. I am just wondering if I should make classes instead of functions? What would be the benefit of that? Do you know? [/snip] You should keep classes and functions seperate in your thought process, they are not the same and not meant to do or be the same thing. Here is a good thought process to apply to classes... 1. Is this an object? If the answer is no, you do not need a class. 2. Do I need to do something (have an action or set of actions performed), perhaps over and over again? If the answer is yes, this is a function. A method is a function that applies to a specific class of objects. If you created a class and methods for each item you code would be bloated, slow, and hard to maintain. Do not think of functions and classes in the same light, you will make yourself more confused. One is definitely not the other. Jay * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
Is there an advantage to Classes vs. Functions? Jay Blanchard [EMAIL PROTECTED] wrote in message 000c01c22cdb$11485c10$8102a8c0@niigziuo4ohhdt">news:000c01c22cdb$11485c10$8102a8c0@niigziuo4ohhdt... [snip] example? [/snip] http://www.devshed.com/Server_Side/PHP/FormValidatorClass/page1.html Good tutorial Jay * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
urn true; } } function selectquery() { global $TRUE, $FALSE; if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-qry = mysql_query($this-sql); if (!$this-qry) { return false; } else { $this-numberrows = mysql_num_rows($this-qry); if ($this-numberrows 0) { for($x = 0; $x $this-numberrows; $x++) { $this-result[$x] = mysql_fetch_row($this-qry); } } else { //echo([Error:] Retrieving data); return false; } return true; } } function insertquery() { global $TRUE, $FALSE; if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-qry = mysql_query($this-sql); if (!$this-qry) { return false; } else { return true; } } function deletequery() { global $TRUE, $FALSE; if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-qry = mysql_query($this-sql); if (!$this-qry) { return false; } else { return true; } } function updatequery() { global $TRUE, $FALSE; if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-qry = mysql_query($this-sql); if (!$this-qry) { return false; } else { return true; } } } ? And now in any .php file you include this .obj file and use it as follows. $db1 = new mysqldb(); // create a new instance of the mysql object. // You can create as many as you want and the great thing is // that each instances vars will contain there own data. $sql = Insert into ...; $db1-setTransactionSwitch(true); $db1-begin(); //this will begin a transaction (InnoDb or BDB tables are required for this) $db1-setsql($sql); //set the objects $sql variable. if (!$db1-insertquery()) //now call the method that does all the work so $db1-setTransactionSwitch(false); //you don't have to recode it. If it fails roolback. if($db1-getTransactionSwitch()==false);{ $db1-rollback(); }else{ $db1-commit(); } Classes allow you to have multple instances of an object containing seperate info, makes it easier to keep track of. Hope this helps, Steve. -Original Message- From: Chris Crane [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 16, 2002 11:07 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Classes vs. Functions It helps a little bit, thank you. Could you provide some code as to what a Class looks like. I am just trying to understand it better and if I see it, it might help. Jay Blanchard [EMAIL PROTECTED] wrote in message 000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt... [snip] Could someone please explain the difference between classes and functions and how to use a class. I write alot of PHP, but I never understood this at all. I use an include statement in many of my pages and include a file with a bunch of functions. For instance, I might have a function called stock(); In the page I am using I include the file that has this function and I call it like this: stock($Sym); I am wondering if I am doing it the wrong way. So I need to better understand classes. What is one, and why would you use it? [/snip] A class is the representation of an object, such as a person, place, or thing. A function is a group of commands that can be called for a specific purpose. function addNumbers() A function can be performed on an object, but an object (the logical extension of class) cannot be performed on a function. Does that help? Jay Cleverly disguised as a responsible adult * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes vs. Functions
Yes any vars used in a class have to be defined ahead. Then just write quick set and get mothods to change them. And the important thing to remember is $db1-Host; retrieved with $db1-getHost(); for example contains seperate data then $db2-$Host; Good luck. -Original Message- From: Chris Crane [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 16, 2002 11:40 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Classes vs. Functions Wow...this is pretty cool. Do you HAVE to declareall your varibles ahead of time? Steve Bradwell [EMAIL PROTECTED] wrote in message 57A1618E7109D311A97D0008C7EBB3A1010C8258@KITCHENER">news:57A1618E7109D311A97D0008C7EBB3A1010C8258@KITCHENER... Classes are great for code reusability, I heavily use a MySQL object or class to make all my conections to a mysql database now and I have included methods for Transactions. Its a long one but its the class I use the most and is a great example of what they are good for. ? class mysqldb { //so here define the name of the class. //set up the object, these are the variables that are accessible by each instance you create // of an object. var $host; var $db; var $dbuser; var $dbpassword; var $sql; var $numberrows; var $dbopenstatus; var $dbconnection; var $qry; var $result; var $TransactionSwitch; /* Use these functions to get and set the values of this object's variables. This is good OO practice, as it means that datatype checking can be completed and errors raised accordingly. */ // Property Get Set these methods are used set class vars and to retrieve them. function gethost() { return $this-dbhost; } function sethost($req_host) { $this-dbhost = $req_host; } function getdb() { return $this-db; } function setdb($req_db) { $this-db = $req_db; } function getdbuser() { return $this-dbuser; } function setdbuser($req_user) { $this-dbuser = $req_user; } function getdbpassword() { return $this-dbpassword; } function setdbpassword($req_password) { $this-dbpassword = $req_password; } function getsql() { return $this-sql; } function setsql($req_sql) { $this-sql = $req_sql; } function getnumberrows() { return $this-numberrows; } function setnumberrows($req_numberresults) { $this-numberesults = $req_numberresults; } function setdbconnection($req_dbconnection) { $this-dbconnection = $req_connection; } function getdbconnection() { return $this-dbconnection; } function setTransactionSwitch($switch) { $this-TransactionSwitch = $switch; } function getTransactionSwitch() { return $this-TransactionSwitch; } /* This is the constructor for the object. In this case I have set the initial values of a number of the object properties to those values declared in the global constants.inc. By doing this, I only need to change the values of these properties for specific operations, which we will not need to do throughout this example */ function mysqldb() { global $HOST, $DB, $WEBUSER, $WEBPASSWORD; global $TRUE, $FALSE; $this-sethost($HOST); $this-setdb($DB); $this-setdbuser($WEBUSER); $this-setdbpassword($WEBPASSWORD); $this-setdbconnection($FALSE); } /* These are the methods for the object. They provide for opening a connection to the database, closing a connection and executing a SELECT query. Of course, these can be expanded upon to allow for INSERT's, UPDATE's and DELETE's etc... */ function opendbconnection() { global $TRUE, $FALSE; $this-dbconnection = mysql_connect($this-dbhost, $this-dbuser, $this-dbpassword); if ($this-dbconnection == $TRUE) { $this-db = mysql_select_db($this-db); $this-setdbconnection($TRUE); } else { $this-setdbconnection($FALSE); return false; } return true; } function closedbconnection() { if ($this-dbconnection = $TRUE) { mysql_close($this-dbconnection); } } function begin( ) { if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-setsql(BEGIN); $this-qry = mysql_query($this-sql); if (!$this-qry) { return false; } else { return true; } } function rollback( ) { if ($this-dbconnection == $FALSE) { $this-opendbconnection(); } $this-setsql(ROLLBACK); $this-qry = mysql_query($
Re: [PHP] Classes vs. Functions
ðÒÉ×ÅÔ! Martin Clifford wrote: Could someone please explain the difference between classes and functions and how to use a class. Well, that's a 1 billion $$ question. I don't think one can fully grasp that difference by reading an email. I strongly suggest you to buy yourself a book about OOP and have a go at it. That is, if you really care about knowing. Whether you should like OOP or not is a religious matter, so I will not enter the field. OOP has its pluses and its minuses. It's a technique, not an ultimate truth, although it is often presented as such. And as any technique, it can do wonders and it can do plain bull**t when not properly used. Nowadays 100% of my work is OOP based, but I worked some 15 years on functions and I cannot blame those who keep working that way. There are reasons for doing it and reason for not to do it. Get yourself a good clear book, then make a decision. ÐÏËÁ áÌØÂÅÒÔÏ ëÉÅ× -_=}{=_-@-_=}{=_--_=}{=_-@-_=}{=_--_=}{=_-@-_=}{=_--_=}{=_- LoRd, CaN yOu HeAr Me, LiKe I'm HeArInG yOu? lOrD i'M sHiNiNg... YoU kNoW I AlMoSt LoSt My MiNd, BuT nOw I'm HoMe AnD fReE tHe TeSt, YeS iT iS ThE tEsT, yEs It Is tHe TeSt, YeS iT iS ThE tEsT, yEs It Is... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes vs. Functions
Martin Clifford wrote: Could someone please explain the difference between classes and functions and how to use a class. Whether you should like OOP or not is a religious matter, so I will not enter the field. OOP has its pluses and its minuses. It's a technique, not an ultimate truth, although it is often presented as such. And as any technique, it can do wonders and it can do plain bull**t when not properly used. Nowadays 100% of my work is OOP based, but I worked some 15 years on functions and I cannot blame those who keep working that way. There are reasons for doing it and reason for not to do it. Get yourself a good clear book, then make a decision. [snip] I have to agree here. IMO, there are benefits in using classes over functions, and there's benefits in using functions over classes. Once you get to know oop a little better, you should be able to determine which is better is any given situation. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes vs. Functions
On 17 Jul 2002 at 12:43, Michael Hall wrote: There is no simple answer here. I have started using classes where I find I am writing a lot of related functions that share similar parameters. Database connection and queries are a good example. Authentication is another. Yeah. I have another class that builds forms, because I just hate the tedium of coding HTML forms by hand. It is really just a collection of functions, though, and could work fine as such. This is a gray area imho. I'd leave all html to the person who cares what it looks like, not what it does. I usually also use a code generator to create html and their forms but they are a separate layer. I use templates, wish a lot more php people would as well although I've seen some weird stuff where in this one bb they store templates in the database. That's interesting. I'm still learning/exploring ... I am always guided by the principle that whatever makes less work for me (but achieves the same result) is probably a good thing. IMHO classes are best for more universal code that really can be used in many different places. My functions tend to be more application specific. Yeah, can't say too much more than that. There is the style of coding where one application is completely independent of another. Then you begin to realize, gee ... I could just cut and paste this code. And then there's always the funny repetion of the exact same code every 30 lines or so (depending on memory of programmer I guesss). Eventually you begin to realize gee ... could I put this stuff in a library. A CLASS after all is just a collection of functions with a data model. But ... there is modular and then there is OO imho. I'm a die hard modular programmer who is trying to think in a more OO way. But of course when you just gotta get something done, do it. The value in spending a bit more time going the modular/OO route is that your application will be easier to evolve and debug. Peter-- http://www.readbrazil.com/ Answering Your Questions About Brazil -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Classes vs. Functions
[snip] A CLASS after all is just a collection of functions with a data model. But ... there is modular and then there is OO imho. [snip] A class is more than just a bunch of functions that have been placed together. If you want to do that, then you might as well just throw them all into the same include file and that's it. There's no added benefit in wrapping a class around them. Agreed, a class can be used to simulate the C-style struct, or to collect a bunch of functions, but the real power of classes is when you start to model the real world objects (ie, methods and properties, not just functions and variables.) I'm not saying to go all OO (or all procedule, I use both...), just that when used correctly, they can be really helpful. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Classes and functions
Hy, I'm writing a class for POP3 access and I want some of the internal functions to be private, like the mime decoding stuff. Is there any way to do this? I found nothing in the docs :( Daniel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Classes and functions
There is something like RFC on this URL :http://www.zend.com/engine2/ZendEngine-2.0.pdf What is supposed to be new in Zend Engine 2.0 . private members, and other things. But now everything is public. Why? Because every variable in PHP is zval (zend value).PHP is built on the top of Zend using Zend API. Simply the structure of zval is not suitable for private members. The cast from array to object and vice versa is possible. When you cast array to object your receive an instace with properties - array_keys() with correspoding values - array_values(). When you cast from object to array, the names of the properties are key names, the values of the keys - values. From zend.h /* * zval */ typedef struct _zval_struct zval; typedef struct _zend_class_entry zend_class_entry; typedef struct _zend_object { zend_class_entry *ce; HashTable *properties; } zend_object; typedef union _zvalue_value { long lval; /* long value */ double dval;/* double value */ struct { char *val; int len; } str; HashTable *ht;/* hash table value */ zend_object obj; } zvalue_value; struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uchar type; /* active type */ zend_uchar is_ref; zend_ushort refcount; }; Regars, Andrey Hristov IcyGEN Corporation http://www.icygen.com BALANCED SOLUTIONS - Original Message - From: Daniel Reichenbach [EMAIL PROTECTED] To: Php-General [EMAIL PROTECTED] Sent: Wednesday, November 28, 2001 12:17 PM Subject: [PHP] Classes and functions Hy, I'm writing a class for POP3 access and I want some of the internal functions to be private, like the mime decoding stuff. Is there any way to do this? I found nothing in the docs :( Daniel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]