|
Standish posted:It has no set meaning, but it's often used to denote an class member or an internal function i.e. anything that shouldn't be directly used except by the original author of the code. That's what I was looking for. Thanks!
|
# ? Mar 27, 2008 17:41 |
|
|
# ? May 16, 2024 18:29 |
|
Tap posted:I've recently taken up PHP as my language of choice for learning how to program. It's a kludge of sorts. Until PHP5 there was no way to make a variable or method private, meaning inaccessible to other objects/functions/etc so people would prepend the name with an underscore to tell other people not to use it directly. It won't stop them from doing so, but it suggests the user should do otherwise.
|
# ? Mar 27, 2008 18:44 |
When I am validating fields submitted from a form I end up with a big if/else like:php:<? if (!aValid) { //error information } else { if (!bValid) { //error information } else { if (!cValid) { //error information } else { //db interaction } } }?> fletcher fucked around with this message at 19:27 on Mar 27, 2008 |
|
# ? Mar 27, 2008 19:21 |
|
fletcher posted:When I am validating fields submitted from a form I end up with a big if/else like: Whatever you do don't use an if-tree. Can you iterate through an array of testcases/functions and then break? Try using a break or a GOTO of some sort.
|
# ? Mar 27, 2008 19:40 |
|
fletcher posted:
php:<? $a=array(aValid,bValid,cValid); $b=array("a error","b error","c error"); function test($a,$b) { for($i=0;$i<count($a);$i++)if(!$a[$i])return $b[$i]; return "all clear!"; } ?>
|
# ? Mar 27, 2008 19:43 |
functional posted:
How do I put my test cases in the array? Let's say I'm doing a ctype_digit() on aValid and making sure it's < someValue. Or do I do that before and just stuff the result in the array? fletcher fucked around with this message at 19:51 on Mar 27, 2008 |
|
# ? Mar 27, 2008 19:48 |
|
fletcher posted:When I am validating fields submitted from a form I end up with a big if/else like: php:<? if (!aValid) { //error information } else if (!bValid) { //error information } else if (!cValid) { //error information } else { //db interaction }?>
|
# ? Mar 27, 2008 20:04 |
|
fletcher posted:How do I put my test cases in the array? Let's say I'm doing a ctype_digit() on aValid and making sure it's < someValue. Or do I do that before and just stuff the result in the array? If your tests are not time intensive, why not simply run all of them and then check at the end to see if one failed? This is what I usually do. Actually, I keep a stack of error messages and then check to see if it's non-empty. php:<? $err=$array(); if(!$aValid) $err[]="a failed"; if(!$bValid) $err[]="b failed"; if(!$cValid) $err[]="c failed"; if(count($err)) echo "errors"; else echo "we're good"; ?> There are some function pointer things you can do, but to be honest it already looks like your situation is too complex for them, and it's a hassle in PHP anyway. Congratulations, you're smarter than your programming language. functional fucked around with this message at 20:13 on Mar 27, 2008 |
# ? Mar 27, 2008 20:08 |
|
functional posted:If your tests are not time intensive, why not simply run all of them and then check at the end to see if one failed? This is what I usually do. Actually, I keep a stack of error messages and then check to see if it's non-empty. Another way to do this is to iterate over the post array in a loop and use a switch statement like this php:<? $errors = array(); foreach($_POST as $key=>$value){ switch($key){ case 'email': //fancy email validation code break; case 'ssn': //fancy ssn validator break; default: if(empty($value)){ $errors[$key]=$key.' cannot be empty'; } break; } } if(empty($errors)){ //process post } else{ //handle errors } ?>
|
# ? Mar 27, 2008 20:39 |
|
(I would prefer a string solution to this, or something built into the PHP library, and not some big package I have to install.) I have a string which consists of potentially bad XML. I have a tag <mytag> which may exist in the XML multiple times. I want to return the text contained in first instance of <mytag>, or the empty string if it doesn't exist. So in the following instance: php:<? <html> <mytag>THIS_IS_THE_STRING</mytag> <span>aaaaaa</span> <html> ?> Is there something that already does this or do I have to write it myself?
|
# ? Mar 27, 2008 21:09 |
|
functional posted:(I would prefer a string solution to this, or something built into the PHP library, and not some big package I have to install.) http://us.php.net/simplexml
|
# ? Mar 27, 2008 21:12 |
|
bt_escm posted:http://us.php.net/simplexml Thank you functional fucked around with this message at 21:32 on Mar 27, 2008 |
# ? Mar 27, 2008 21:13 |
Thanks for all the replies guys, very helpful and appreciated. Another question, is it a bad idea to use an existing php function name as the name of a method in a class? Like if I want to use $obj->delete(), wasn't sure if that's frowned upon.
|
|
# ? Mar 27, 2008 22:02 |
|
fletcher posted:Thanks for all the replies guys, very helpful and appreciated.
|
# ? Mar 28, 2008 01:16 |
|
MononcQc posted:Well, if it's anything that can be confusing to someone else looking over your code, I'd say it's not a good idea. In this case, it's 99% sure to spark some confusion, so I'd go against it just because of that. I disagree. One of the benefits of using classes is the ability to use simple, descriptive member names that won't collide with the same name in other contexts. If $obj->delete() is an appropriate name for what the function does, I'd use it. The fact that it's obviously an object member instead of a global function should eliminate any confusion.
|
# ? Mar 28, 2008 06:02 |
|
DaTroof posted:I disagree. One of the benefits of using classes is the ability to use simple, descriptive member names that won't collide with the same name in other contexts. If $obj->delete() is an appropriate name for what the function does, I'd use it. The fact that it's obviously an object member instead of a global function should eliminate any confusion. I agree, though delete() isn't even a built-in function...
|
# ? Mar 28, 2008 06:20 |
|
admiraldennis posted:I agree, though delete() isn't even a built-in function... I tend to shy away from reusing built-in functions because it makes the syntax highlighting in my editor look odd. All built-in functions are highlighted in blue, and all others are in grey, so it looks kinda strange to see one of my functions in blue. This usually isn't a problem as I use a camelCase naming convention, so overlap is very rare.
|
# ? Mar 28, 2008 06:33 |
|
minato posted:Yes it is: http://jp2.php.net/manual/en/function.delete.php Good job reading the page you linked quote:This is a dummy manual entry to satisfy those people who are looking for unlink() or unset() in the wrong place.
|
# ? Mar 28, 2008 06:59 |
|
functional posted:(I would prefer a string solution to this, or something built into the PHP library, and not some big package I have to install.) From the looks of it, you are parsing HTML which in a lot of cases can be malformed. A great extension for helping with that though is Tidy as it can cleanup and repair broken elements. SimpleXML is a nice interface but you also have the option of using the DOM interface for interacting with XML/HTML which can sometimes give a little more flexibility. I find myself using XPath a lot for pulling content out of HTML.
|
# ? Mar 28, 2008 11:37 |
|
As bad as it may sound, if you are only trying to extract text with a certain pattern, it might be easier to use a regular expression rather than a xml parser. I.e /<mytag>([^<]*)<\\/mytag>/ tef fucked around with this message at 13:24 on Apr 1, 2008 |
# ? Mar 28, 2008 12:21 |
|
Don't you mean /<mytag>([^<]*)</mytag>/
|
# ? Mar 28, 2008 14:10 |
|
DaTroof posted:I disagree. One of the benefits of using classes is the ability to use simple, descriptive member names that won't collide with the same name in other contexts. If $obj->delete() is an appropriate name for what the function does, I'd use it. The fact that it's obviously an object member instead of a global function should eliminate any confusion. I just hate it when the syntax highlight goes crazy on it and highlights it as a built-in function, which then confuses me. I can tend to be distracted, so I make what's necessary to reduce my mistakes and make it easier to read for me
|
# ? Mar 28, 2008 15:16 |
|
Bonus posted:Don't you mean /<mytag>([^<]*)</mytag>/ Should you not escape the forward slash in the closing tag?
|
# ? Mar 28, 2008 18:29 |
|
Kaluza-Klein posted:Should you not escape the forward slash in the closing tag? Yes, you should. However the forums like to eat escaping slashes.
|
# ? Mar 28, 2008 18:39 |
What's the point of declaring public variables in a class? If I remove them all, my application will behave the same, correct?php:<? class User { public $id; public $username; } ?>
|
|
# ? Mar 28, 2008 19:25 |
|
I was wondering if there was a way to use phpmail to send an email, to be recognized by outlook calendar. I have an event that someone would sign up for, and I want to send the email to the user, that states they have signed up for this event. But I want outlook or other mail client to be able to add it to the users calendar.
|
# ? Mar 28, 2008 19:54 |
|
fletcher posted:What's the point of declaring public variables in a class? If I remove them all, my application will behave the same, correct? Not quite sure what you mean by that. Basically public fields are accessible from any code: php:<? class Foo { public $baz; } class Bar { private $baz; } $foo = new Foo; $bar = new Bar; $foo->baz = 'baz'; // No problem here. $bar->baz = 'baz'; // Fatal error! ?> Inquisitus fucked around with this message at 19:57 on Mar 28, 2008 |
# ? Mar 28, 2008 19:54 |
|
fletcher posted:What's the point of declaring public variables in a class? If I remove them all, my application will behave the same, correct? With the way php currently handles objects there's no difference. However removing public or even not declaring the variables is just plain bad practice and could possibly break in future versions of php.
|
# ? Mar 28, 2008 20:08 |
|
bt_escm posted:With the way php currently handles objects there's no difference. However removing public or even not declaring the variables is just plain bad practice and could possibly break in future versions of php. Correct. In PHP5, if a scope isn't declared it's assumed to be public because there was no scope in PHP4.
|
# ? Mar 28, 2008 20:27 |
bt_escm posted:With the way php currently handles objects there's no difference. However removing public or even not declaring the variables is just plain bad practice and could possibly break in future versions of php. What's a good way to maintain my public variable declarations if they are all the exact same as my field names in the database, or do I have to just do it manually?
|
|
# ? Mar 28, 2008 20:47 |
|
fletcher posted:What's a good way to maintain my public variable declarations if they are all the exact same as my field names in the database, or do I have to just do it manually? The simplest way is to do it manually. Alternative you could use an ORM package. What I done in the past is build an array of the column definitions like this php:<? $props = array( 'username'=>array( 'type'=>'varchar', 'length'=>40, 'required'=>1), 'email'=>array( 'type'=>'varchar', 'length'=>255, 'required'=>0, 'validator'=>'email' ) ); ?> Another thing I've done in the past is use a script that I call one time to generate the above listed array after I've designed my database. It saves a little time. bt_escm fucked around with this message at 21:09 on Mar 28, 2008 |
# ? Mar 28, 2008 21:00 |
bt_escm posted:
Wow I like that way. That makes validating all the input so much easier, thanks!
|
|
# ? Mar 28, 2008 21:07 |
|
I'm writing some authentication code for something I'm mucking around with, while at the same time trying to improve my coding habits. With the example below, CheckPassword() throws an exception if the password is incorrect. For readabilities sake, should CheckPassword() have a boolean return and form an if block around the rest of the code, even though it won't change anything? php:<? public function SetPassword($oldPassword, $newPassword, $newPassword2) { if ($newPassword != $newPassword2) { throw new PasswordMismatchException(); } $this->CheckPassword($this->name, $oldPassword); // Better? // if ($this->CheckPassword($this->name, $oldPassword)) // { $salt = self::CreateSalt(); $password = self::CreateSaltedHash($newPassword, $salt); $sql = 'UPDATE user SET password = "' . DB::Escape($password) . '" salt = "' . DB::Escape($salt) . '" WHERE name = "' . DB::Escape($this->name) . '"'; return $this->db->Execute($sql); // } // return false; } ?> Finite fucked around with this message at 11:04 on Mar 29, 2008 |
# ? Mar 29, 2008 02:28 |
|
Finite posted:It looks like you're using a database abstraction layer. Use its built in prepared query functions instead of string concatenations. It also looks like you're using PEAR::DB, any chance you can upgrade to MDB2?
|
# ? Mar 29, 2008 02:34 |
|
Finite posted:I'm writing some authentication code for something I'm mucking around with, while at the same time trying to improve my coding habits. With the example below, CheckPassword() throws an exception if the password is incorrect. What you want to use is a try/catch around the the code after the newPassword to newPassword2 check. Also your example seems really strange since you are mixing oop and procedural function calls.
|
# ? Mar 29, 2008 06:16 |
|
duz posted:It looks like you're using a database abstraction layer. Use its built in prepared query functions instead of string concatenations. It's not, it's just a database object I wrote which does some of the tedious work for me. I know that I need to look at it, but it works fine at the moment and I hadn't planned on looking at it again just yet as I'm already working on a few other things. bt_escm posted:What you want to use is a try/catch around the the code after the newPassword to newPassword2 check. Ok. The OOP/Procedural crap is fixed. That'll teach me to post before testing for obvious errors... I didn't want to bother using a try/catch as it's not really necessary. I want the exception to be thrown as it comes out as an InvalidPasswordException, which I think speaks for itself. My problem was whether the code was readable in it's current state, or if the call to CheckPassword() is confusing as it has no obvious way to fail. Then again, I'm not sure if that's the best way to do this either, and it should just return true/false and I can work from there. It's more object design than PHP Finite fucked around with this message at 11:06 on Mar 29, 2008 |
# ? Mar 29, 2008 11:03 |
|
I still think a try catch would be better here because it could tell you why it failed, but after reading over your example some more the if statement would be fine. I think this would work a lot better for you for logging purposes php:<? try{ $this->CheckPassword($this->name, $oldPassword); $salt = self::CreateSalt(); $password = self::CreateSaltedHash($newPassword, $salt); $sql = 'UPDATE user SET password = "' . DB::Escape($password) . '" salt = "' . DB::Escape($salt) . '" WHERE name = "' . DB::Escape($this->name) . '"'; return $this->db->Execute($sql); } catch(SPECIFIC EXCEPTION $e) { } catch(Exception $e){ } ?>
|
# ? Mar 29, 2008 14:18 |
|
I have since upgrade to PHP 5 and it seems my method of querying a server via a predefined URL no longer works:code:
|
# ? Mar 29, 2008 21:45 |
|
Treytor posted:I have since upgrade to PHP 5 and it seems my method of querying a server via a predefined URL no longer works: check you fopen wrapper settings. also http://us3.php.net/manual/en/function.stream-context-create.php
|
# ? Mar 29, 2008 22:38 |
|
|
# ? May 16, 2024 18:29 |
|
Yeah, to be honest I wasn't even using fopen before. Thanks!
|
# ? Mar 29, 2008 23:42 |