Re: [PHP] One class, many methods: PHP4.x: Q's
Hi Eric! Thanks for your reply. :) Eric Butera wrote: If at all possible you're going to want to use PHP5 if you're doing ...snip... of those problems have been alleviated. Gosh! I wish I could use PHP5... Unfortunately the hosting company I am using has not upgraded. I can not wait to start learning PHP5 OOP! Also I would recommend always defining all of the class variables that ...snip... variables and all of that. Great! Good to hear. Thanks for the clarification. Very helpful. :) The get and set method's you wrote are part of the concept of encapulation. Read this: http://en.wikipedia.org/wiki/Information_hiding for more information. Excellent! Thanks for link, very interesting. Man, I love programming, I just wish I were better at it! Lol. Anyway, that is why I am on this list... Feedback from the pros is great! Many thanks Eric and all! I greatly appreciate your help. Have a good one! Cheers, Micky -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
On 5/10/07, Micky Hulse [EMAIL PROTECTED] wrote: Micky Hulse wrote: Ah, well Chapter 6, page 144-145 have helped clear-up a little bit of my confusion: Sorry, forgot to mention what book I was refering to: O'Reilly Programming PHP By Rasmus Lerdorf Kevin Tatroe http://snipurl.com/1k4ug -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If at all possible you're going to want to use PHP5 if you're doing OOP. I use it all the time in 4, but you're going to have to deal with references until you are sick and it is just annoying when most of those problems have been alleviated. Also I would recommend always defining all of the class variables that it will possibly use. Some of the code I write lives for years and working over dozens of different projects it is impossible to remember everything completely. Also it will help avoid bugs such as undefined variables and all of that. The get and set method's you wrote are part of the concept of encapulation. Read this: http://en.wikipedia.org/wiki/Information_hiding for more information. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] One class, many methods: PHP4.x: Q's
Hi, This might be a real obvious question/answer... but I have been up all night geeking on code, so my mind is kinda mushy -- please be kind. :) I have a class called Randimg. Within that class, I have several methods. Within a few of those methods, I use the below code snippet: $_SERVER['DOCUMENT_ROOT'] What would be the best way to assign the above code to a global variable/constant within the class? Also, within each method, I am returning error strings... I would like to create these as class constants... Or something... What is best way to setup static strings within my class? Basically, I would like to have these strings at the top of my class so potential users can easily change the settings. ;) Lol, sorry is stpid questions... I am still kinda new to PHP OOP. Many TIA's! Cheers, Micky -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
Micky Hulse wrote: I have a class called Randimg. Within that class, I have several methods. Within a few of those methods, I use the below code snippet: $_SERVER['DOCUMENT_ROOT'] What would be the best way to assign the above code to a global variable/constant within the class? Within the constructor for Randimg you could set a local protected variable: class Randimg { var $docroot = false; function Randimg () { $this-docroot = $_SERVER['DOCUMENT_ROOT']; } and then you can just reference $this-docroot from any of the Randimg methods. In PHP 5 could you create $docroot as a protected class constant, but in PHP 4 the above will have to do. Cheers, Rich -- Zend Certified Engineer http://www.corephp.co.uk Never trust a computer you can't throw out of a window -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
On 5/10/07, Micky Hulse [EMAIL PROTECTED] wrote: Hi, This might be a real obvious question/answer... but I have been up all night geeking on code, so my mind is kinda mushy -- please be kind. :) I have a class called Randimg. Within that class, I have several methods. Within a few of those methods, I use the below code snippet: $_SERVER['DOCUMENT_ROOT'] What would be the best way to assign the above code to a global variable/constant within the class? Also, within each method, I am returning error strings... I would like to create these as class constants... Or something... What is best way to setup static strings within my class? Basically, I would like to have these strings at the top of my class so potential users can easily change the settings. ;) Lol, sorry is stpid questions... I am still kinda new to PHP OOP. Many TIA's! Cheers, Micky -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php As Rich pointed out you cannot have constants within a class in PHP4. To answer the second part of your mail, one technique that I have seen is something like the code below. define('FOO_FILE_NOT_FOUND', 1); define('FOO_FILE_NOT_READABLE', 2); define('FOO_FILE_INVALID PATH', 2); class Foo { function render() { if (! is_readable(...)) { return FOO_FILE_NOT_READABLE; } } } Another way that is nice is Solar's way of handling locales. Basically it uses strings such as 'SOME_ERROR_MESSAGE' which is populated by an include file that contains the actual value to it. So in this include file you create an array that contains key value pairs of error codes and error messages. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
Hi Rich! Thanks for the quick reply. I really appreciate your help. :) Richard Davey wrote: In PHP 5 could you create $docroot as a protected class constant, but in PHP 4 the above will have to do. Ahhh! Very interesting. Many thanks for the code example and explanation -- that approach works for me. I assume setting basic strings (i.e. settings) for my error messages would be best done via: class Randimg { var $msg_1 = 'This is a string'; var $msg_2 = 'This is another string'; function rand_img() { echo $this-msg_1; } function foo() { echo $this-msg_2; } } It just seems so extraneous to declare those strings as vars... A class constant sounds like the best approach. I can't wait to start using PHP5! :D Thanks again Rich! I really appreciate your time and expertise. Have a great day and/or night. Cheers, Micky -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
Hi Eric! Thanks for the fast reply. I really appreciate the help on this one. :D Eric Butera wrote: As Rich pointed out you cannot have constants within a class in PHP4. To answer the second part of your mail, one technique that I have seen is something like the code below. Ahhh, that is interesting! Thanks for sharing the code snippet. I think I may head this direction. :) Another way that is nice is Solar's way of handling locales. Basically it uses strings such as 'SOME_ERROR_MESSAGE' which is populated by an include file that contains the actual value to it. So in this include file you create an array that contains key value pairs of error codes and error messages. Oh, wow! That sounds interesting... I am going to catch some zzZZzz's and read-up on this in the morning. :D Ahhh, and it looks like a cool framework too: http://solarphp.com/ I may have to dip-into that. I have yet to try a PHP framework. Django was pretty fun, but I think I would have more fun with a PHP framework. :) Anyway, thanks a billion for sharing different approaches, it is great to hear how the pros deal with such things. Have a great day and/or night. Cheers, Micky -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
Micky Hulse wrote: I assume setting basic strings (i.e. settings) for my error messages would be best done via: class Randimg { var $msg_1 = 'This is a string'; var $msg_2 = 'This is another string'; function rand_img() { echo $this-msg_1; } function foo() { echo $this-msg_2; } } I don't know if that's the *best* way, personally I'd not really do it like that, but no-one can tell you it is 'right' or 'wrong' because it depends on your application. For example if you only have a handful of possible error messages (say 10) then there is nothing inherently wrong with the above approach. If you're talking about lots of error messages then under PHP 4 I'd declare them as consts in a required() file. define('APP_ERROR_1', 'This is an error message'); Then just use APP_ERROR_1 in the methods that need it. It's a bit messy because you are creating consts all over the place, but it's an option for you and at least it keeps them all in a single file and out of the header of your class. Another way might be to create a specific class that does nothing but handle error messages / responses, then call that. Cheers, Rich -- Zend Certified Engineer http://www.corephp.co.uk Never trust a computer you can't throw out of a window -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
Richard Davey wrote: I don't know if that's the *best* way, personally I'd not really do it like that, but no-one can tell you it is 'right' or 'wrong' because it ...snip... Perfect answer! Thanks! I do not have too many error messages to store, but I do like the constant approach... Looks like I have a bit of testing to do tomorrow (brain is mush atm.) Thanks again Rich and Eric. :) Cheers, M -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
Richard Davey wrote: Within the constructor for Randimg you could set a local protected variable: class Randimg { var $docroot = false; function Randimg () { $this-docroot = $_SERVER['DOCUMENT_ROOT']; } Hi Rich and all, just one other quick question... Which approach below would you choose? Example 1: class Randimg { # Initialize var variables: var $foo1 = FALSE; var $foo2 = FALSE; var $foo3 = TRUE; # Constructor: function Randimg { $this-foo1 = TRUE; $this-foo2 = TRUE; $this-foo3 = FALSE; return $this-method_foo(); } function method_foo() { $x = $this-$foo1; $y = $this-$foo2; $z = $this-$foo3; /* Do stuff... */ return $x.$y.$z; } } Example 2: class Randimg { # Constructor: function Randimg { $foo1 = TRUE; $foo2 = TRUE; $foo3 = FALSE; return $this-method_foo($foo1, $foo2, $foo3); } function method_foo($x, $y, $z) { /* Do stuff... */ return $x.$y.$z; } } Hopefully my syntax is decent... I am guessing the approach I take depends on the needs of my script. Setting local protected variables seems like it would work well for my latest project, but is that bad programming practice to rely upon local protected variables when it comes to passing variables between methods? Seems like I have always read/heard that it is best to explicitly pass variables as function parameters... vs throwing around globals. Sorry if silly questions. :) Thanks again! Cheers, M -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
Micky Hulse wrote: Seems like I have always read/heard that it is best to explicitly pass variables as function parameters... vs throwing around globals. Ah, well Chapter 6, page 144-145 have helped clear-up a little bit of my confusion: ...snip... Here's a simple class definition of the Person class that shows the $this variable in action: class Person { var $name; function get_name() { return $this-name; } function set_name($new_name) { $this-name = $new_name; } } As you can see, the get_name() and set_name() methods use $this to access and set the $name property of the current object. ...snip... Declaring Properties In the previous definition of the Person class, we explicityly declared the $name property. Property declaration are optional and are simply a courtesy to whoever maintains your program. It's good PHP style to declare your properties, but you can add new properties at any time. ...snip... You can assign default values to properties, but those default values must be simple constants: var $name = 'J Doe'; // Works var $age = 0; // Works var $day = 60*60*60; // Doesn't work. ...snip... So, I think I was getting a bit confused with the var variables... But now it looks like it is not bad practice to get and set object properties, and it almost sounds like it is good practice (though, not required) to declare these properties (var variables) for other eyeballs/programmers. Man, I love programming in PHP. I just wish I were better at it! :D K, gonna go practice some on these new techniques. Cheers, Micky -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] One class, many methods: PHP4.x: Q's
Micky Hulse wrote: Ah, well Chapter 6, page 144-145 have helped clear-up a little bit of my confusion: Sorry, forgot to mention what book I was refering to: O'Reilly Programming PHP By Rasmus Lerdorf Kevin Tatroe http://snipurl.com/1k4ug -- Wishlists: http://snipurl.com/1gqpj Switch: http://browsehappy.com/ BCC?: http://snipurl.com/w6f8 My: http://del.icio.us/mhulse -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php