|
Golbez posted:I believe it's, the other 25,000 lines of code on the site are all procedural, so why start splitting it up into two camps? Just because some code uses objects, it doesn't mean that it's object-oriented, or that it's not procedural in style. Are they refusing to use objects whatsoever, or just refusing to allow portions of the codebase to be written in completely object-oriented fashion? If they are refusing to use objects at all, maybe you could find a part of the existing codebase that would particularly benefit (i.e. be made a lot simpler and more maintainable) from being refactored to use objects, and make the case for doing that. Or if you can make the case for a new piece of code being written using objects, all the better, since that doesn't involve time being spent rewriting working code. You should emphasise that just because it makes use of objects, doesn't mean it's not procedural, and that objects are sometimes an appropriate tool for the job regardless of what style the code is written in.
|
# ? Sep 10, 2010 23:26 |
|
|
# ? May 31, 2024 05:21 |
|
Hammerite posted:Just because some code uses objects, it doesn't mean that it's object-oriented, or that it's not procedural in style. Are they refusing to use objects whatsoever, or just refusing to allow portions of the codebase to be written in completely object-oriented fashion? Refusing to allow objects at all. Seeing as how I'm still technically a contract worker and not a full-time employee, I'm not pushing the issue with the guy who's built this thing for the last four years - but once I have an actual job there, I'll find some way of pushing harder without pestering/sounding like a dick. The best place for what you say would be the db code, which (if you pay attention to my posts, you know) uses mysql, rather than mysqli or PDO. What we have works, so long as we remember to escape everything, but ... I really don't know how to push this further without coming across as, well, pushy.
|
# ? Sep 11, 2010 00:24 |
|
Golbez posted:Refusing to allow objects at all. Seeing as how I'm still technically a contract worker and not a full-time employee, I'm not pushing the issue with the guy who's built this thing for the last four years - but once I have an actual job there, I'll find some way of pushing harder without pestering/sounding like a dick. I guess just continue to drop hints, along the lines of "You know, this code would be a touch simpler if instead of x, we did..."? Give specific examples of where use of objects would provide a benefit. But there likely isn't any scope for you to change anything until you have more standing there.
|
# ? Sep 11, 2010 00:45 |
|
Golbez posted:Refusing to allow objects at all.
McGlockenshire fucked around with this message at 06:52 on Sep 11, 2010 |
# ? Sep 11, 2010 06:50 |
|
McGlockenshire posted:This is a dangerous, dangerous sign. The lead is very likely suffering from one or more of a handful of problems. quote:*Worse, he doesn't have a single, unified version of where his software is going, or he has one and is unable to articulate it. This may be true especially if other coders on the project constantly have to ask for guidance or clarification. quote:*He may have first experienced excessive OO wankery, such as a Java framework that requires a dozen layers of indirection to get anything done, and believes that such dangerous levels of obsessive idiocy is what OO actually is. quote:*And finally, he may not understand OO and is thus afraid of it. This is the worst possible problem, and is frankly the most likely given that you're still using the mysql extension. quote:The project I now lead at work was built by someone that matched parts of this profile. You do not want to work under someone like this. It took the entire team threatening to ragequit (and one doing so) and some major pushing from management before he realized the scale of the problem. He quit. You might not be so lucky. (Then again, I'm not so lucky. I'm responsible for maintaining and now replacing this clusterfuck. Hopefully after I'm long gone, my name won't be cursed weekly as his is...)
|
# ? Sep 11, 2010 19:33 |
|
ok so check this out, I built the three dimensional array:code:
rugbert fucked around with this message at 23:56 on Sep 11, 2010 |
# ? Sep 11, 2010 23:39 |
|
rugbert posted:But what if, in that foreach, i need to also grab the poo poo in that last array (extras) under music? You can use the [code][/code] or [php][/php] BBCode tags to get nicely formatted code in your posts. What do you mean by "grab the poo poo in ..."? You want to assign the $product['extras'] array to a variable? You could do that using code:
|
# ? Sep 11, 2010 23:47 |
|
Hammerite posted:ha sorry, I just woke up. Fixed my post Sorry, I meant to echo those values. Want if I wanted to also display the values in "extras". Would I make another foreach? rugbert fucked around with this message at 23:57 on Sep 11, 2010 |
# ? Sep 11, 2010 23:53 |
|
rugbert posted:ha sorry, I just woke up. Fixed my post How do you want to echo them? If you want them comma-separated, you could use echo implode(', ', $product["extras"]);
|
# ? Sep 12, 2010 00:32 |
|
Hammerite posted:How do you want to echo them? If you want them comma-separated, you could use I was planning on putting them into a select box actually, or into a list. so each item in the "extras" array would be in a list item or an option
|
# ? Sep 12, 2010 00:34 |
|
rugbert posted:I was planning on putting them into a select box actually, or into a list. so each item in the "extras" array would be in a list item or an option Oh, ok. Well, in that case if you are just going to echo it out, using a foreach would work I guess. Something like the following perhaps code:
|
# ? Sep 12, 2010 01:05 |
|
I'm working on a small script for a website. Its my first PHP project, and I need some advice. I have a sign-up form that the user can enter some information. I'd like to spread this info out over several pages. Basically, the user enters a small bit of info, clicks next, and enters more info. Repeat until all info is collected. Should I have one php script do this, or should each page have its own php script?
|
# ? Sep 12, 2010 02:04 |
|
Golbez posted:It's been his for four years, I might be the first new one, I don't know. There's no other coders but us.
|
# ? Sep 12, 2010 05:47 |
|
McGlockenshire posted:This also explains much. Learning to work as a team is a challenge. The funny thing is, I just came off being fired from my OWN baby that I was the sole developer on for five years. So it's not like I have any experience working on a team, which is probably why thus far I've been a doormat.
|
# ? Sep 12, 2010 06:16 |
|
Iron Squid posted:I'm working on a small script for a website. Its my first PHP project, and I need some advice. You could do it all in one page, and I'd say that's probably the simplest option code-maintenance wise - I'd say have the <form> action equal to './thispage.php?Page=2', and read in that $_GET value to determine what to ask the user next. The method of the form would still be POST, and you can drop all of that info into their $_SESSION, which you'll need to session_start() before you send any output to the browser. The most obvious caveat is to sanitise ALL of the $_POST or $_GET variables you get from the client, especially before putting them into the database.
|
# ? Sep 12, 2010 12:11 |
|
Iron Squid posted:I'm working on a small script for a website. Its my first PHP project, and I need some advice. Unless there is some really compelling reason to do this ("it looks cool" is NOT a compelling reason), forms should not be spread out over multiple pages. There are lots of usability studies showing multiple-page forms have much lower completion rates, higher error rates, and frustrate users morethan single page forms.
|
# ? Sep 12, 2010 13:27 |
|
Anyone ever work with calling python scripts through PHP? I want to have PHP present the front-end interface and deal with passing information and have python do grunt work in the background. I am led to believe this is very possible but I'm not sure the right way to go about it. This is all in a standard LAMP environment so having python is not an issue.'' I can do it with exec is there anything I should be worried about? Sylink fucked around with this message at 17:00 on Sep 13, 2010 |
# ? Sep 13, 2010 16:56 |
|
Sylink posted:Anyone ever work with calling python scripts through PHP? You probably want to use http://php.net/manual/en/function.popen.php as it's marginally safer than exec. Also remember to escape poo poo http://us.php.net/manual/en/function.escapeshellarg.php And you'll probably want to write a wrapper to do that stuff so you don't have to check the result for the normal Python errors every single time. Edit: quick example php:<? $pipe = popen("python something.py $escaped $opts $go $here", 'r'); echo chr(10),'<br><pre>> '; while(!feof($pipe)){ $temp = fread($pipe, 1024); echo str_replace("\n", "\n> ", $temp); $result .= $temp; } pclose($pipe); echo '<br></pre>',chr(10); //the system can't find the script to run if((strpos($result, 'something.py: command not found') === false) && //python can't find a file (strpos($result, 'No such file or directory') === false) && //some exception was thrown all the way out (script crashed) (strpos($result, 'Traceback (') === false) && //just in case (strpos($result, 'Error:') === false) && //the parameters changed on us (strpos($result, 'Usage:') === false)) { //feels good, man }else die('BEES!'); ?> Munkeymon fucked around with this message at 21:11 on Sep 13, 2010 |
# ? Sep 13, 2010 21:00 |
|
Exec seems easier and I don't intend to put any direct user commands into it. Maybe I'm dumb but the usage of popen is hard to read for me.
|
# ? Sep 13, 2010 21:40 |
|
If you can daemonize your python, and can install software on your machine, take a look at Gearman. There are python and PHP bindings. It should make it trivial for your PHP code to call defined functions in python-land, without process control, without forking (well, forking PHP), etc.
|
# ? Sep 13, 2010 23:20 |
|
Here's a throwback to the discussion about database interfaces. Maybe it should be in Coding Horrors instead. Long story short, I'm fixing our "home-grown" database abstraction interface and am getting ready to send all of the changes upstream to the open source project (and original devs) that wrote it. It's grown from a "let's switch from mysql functions to an object, yay!" to a "hay, what's this mysqli thing guys?" to "gently caress MySQL, gimme Postgres! Oh, and sqlite too." SQLite support is done through PDO, and both PDO and PG do their own proper placeholder thing. After I got those using native placeholders instead of our manual replacement mechanism, I decided to tackle mysqli's prepared statements. Now, I've always recommended mysqli, because it's mysqli. The I stands for "Improved", right? It's supposed to be better. And it is, in a lot of ways. But I never actually used any of the "improved" methods. Has anyone actually used mysqli's prepared statements? Who designed this poo poo?
What am I talking about? Here, let's look at this code: php:<?php $foo = range(1, 100); $bar = array(); foreach($foo as $baz) $bar[] = &$baz; print_r($bar); PHP references are attached to the container -- the variable itself -- not the value stored in the container. Foreach, the best, fastest way to iterate over an array, reuses $baz in each loop, replacing the value at the start. We just created a list of 100 references to the very last element in $foo. In order to do this correctly, I need to use an alternative loop structure (very easy, enough to make this a non-complaint), or I need to do a bizarre thing to work around foreach's behavior: foreach($foo as &$baz). See what changed? $baz is a reference there, indicating that I want the values in $foo passed into $baz by reference to begin with, thus making our assignment a reference of a reference. This would be a completely situation for someone that didn't understand the hosed nature of PHP references. But it doesn't even matter, because the world explodes the instant I try to stick a string where an integer belongs. I guess I'll stick to our ancient placeholder replacement method, and stop recommending mysqli in favor of just advocating PDO. Shame on you, PHP internals. Shame on you. Especially after you got reamed because internal functions never handle references safely. Hell, you even said you'd go back and fix all the internal functions that take references to fix them to stop this! Why do you hate me, PHP? Why can't I quit you? McGlockenshire fucked around with this message at 06:28 on Sep 14, 2010 |
# ? Sep 14, 2010 05:37 |
|
McGlockenshire posted:
code:
php:<?php $foo = range(1, 100); $bar = array(); foreach($foo as $baz) { $bar[] = $baz; } print_r($bar); ?> code:
|
# ? Sep 14, 2010 14:06 |
|
Cadoc posted:What's different? I do not use the & before $baz. Right. That creates a reference, simulating how mysqli_stmt_bind_param creates references, demonstrating that using foreach to loop over arguments and passing them to the bind breaks things. An alternative loop structure or creating the $baz as a reference in the foreach to begin with fixes that problem, but doesn't stop mysqli's designers from suffering from severe mental incapacitation.
|
# ? Sep 14, 2010 16:26 |
|
You don't have to declare anything in PHP. $i = 5, $s = 'fnord', etc. But I've noticed I tend to declare arrays: php:<? $array = array(); while (whatever) $array[] = 'shazam'; ?>
|
# ? Sep 15, 2010 21:10 |
|
Golbez posted:Is there any drawback to this apart from code clarity? It suddenly occurred to me that it seemed weird to insist on declaring my arrays. It's a good habit. Here's why: php:<?php $foo = array(); if($bar) { while($baz = frob()) $foo[] = $baz->what() } foreach($foo as $argh) echo "Metasyntactic variables {$argh}, don't they?\n"
|
# ? Sep 15, 2010 22:59 |
|
Golbez posted:You don't have to declare anything in PHP. $i = 5, $s = 'fnord', etc. I dunno; I do the same thing. I often find myself doing things like this though: php:<? $a=array(); do_something_that_may_or_may_not_push_values_on($a); foreach($a as $key=>$value){ frob($value); } ?> edit. beaten.
|
# ? Sep 15, 2010 23:03 |
|
Golbez posted:You don't have to declare anything in PHP. $i = 5, $s = 'fnord', etc. With strict errors enabled, referencing an undefined variable triggers a notice. Also, as McGlockenshire demonstrated, it can cause unexpected problems elsewhere and make your code harder to debug. You can get away with it, but it's not best practice.
|
# ? Sep 15, 2010 23:14 |
|
DaTroof posted:With strict errors enabled, referencing an undefined variable triggers a notice. Also, as McGlockenshire demonstrated, it can cause unexpected problems elsewhere and make your code harder to debug. You can get away with it, but it's not best practice. Referencing, yes. Setting to, no. If I did "echo $foo[1]" before declaring $foo, that'd throw a notice. But doing "$foo[1] = 'bar'" won't throw an error, even if $foo hasn't been declared. So far as I know.
|
# ? Sep 16, 2010 04:19 |
|
Golbez posted:Referencing, yes. Setting to, no. If I did "echo $foo[1]" before declaring $foo, that'd throw a notice. But doing "$foo[1] = 'bar'" won't throw an error, even if $foo hasn't been declared. So far as I know. Yeah, you're right. Setting $foo[] or even $foo[1] will quietly declare $foo as an array. But that doesn't change the case where $foo is unset because the loop that would have populated it never iterated.
|
# ? Sep 16, 2010 18:38 |
|
Random question time again, just a thought that popped into my head. The PHP manual says, "Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself." Doesn't that mean that one could include a function call inside the foreach call? The manual says it works only on arrays, but couldn't a function that returns an array count? So if I did foreach(functionthatreturnsarray() => $foo) ... ? Fake edit: Holy poo poo, it works? php:<? function foo() { return array(stuff goes here); } foreach(foo() AS $bar) { echo $bar; } ?>
|
# ? Sep 16, 2010 23:08 |
|
quote:I've never seen this construction anywhere else. Think there's a reason for that? Yup. Versions prior to 5.4 (aka the current trunk) don't have the ability to dereference an array returned from a function without assigning the array to a variable first. Once you get in that habit, it's easy to forget that you can still provide functions that provide arrays to other functions and language constructs that want arrays. Between this and traits, I'm really looking forward to 5.4.
|
# ? Sep 16, 2010 23:16 |
|
Lumpy posted:Unless there is some really compelling reason to do this ("it looks cool" is NOT a compelling reason), forms should not be spread out over multiple pages. There are lots of usability studies showing multiple-page forms have much lower completion rates, higher error rates, and frustrate users morethan single page forms. Actually, I was thinking about this today: How long should a form be before it gets split up into multiple pages. After doing my form, it was no more than twenty questions so I'm probably going to leave it all as one page. On a vaguely related not, HOW DO I USE GLOBAL VARIABLES IN PHP? Specifically, I want to keep a bunch of variables in an include file. Lets call it variables.php. Inside this I have something like: code:
code:
Iron Squid fucked around with this message at 01:37 on Sep 17, 2010 |
# ? Sep 17, 2010 01:22 |
Iron Squid posted:Actually, I was thinking about this today: How long should a form be before it gets split up into multiple pages. After doing my form, it was no more than twenty questions so I'm probably going to leave it all as one page. That should work fine, assuming you use the same variable name. In your example here you mix using $html_title and $title. Also make sure you are including variables.php in index.php.
|
|
# ? Sep 17, 2010 01:28 |
|
fletcher posted:That should work fine, assuming you use the same variable name. In your example here you mix using $html_title and $title. Also make sure you are including variables.php in index.php. Whoops. I was typing that from memory. Fixed now.
|
# ? Sep 17, 2010 02:32 |
Iron Squid posted:Whoops. I was typing that from memory. Fixed now. It should work just fine exactly like you have it. code:
|
|
# ? Sep 17, 2010 02:44 |
|
Err, actually I wrote my code incorrectly. The code looks more like this:code:
|
# ? Sep 17, 2010 04:09 |
|
Iron Squid posted:Err, actually I wrote my code incorrectly. The code looks more like this: You need to import it from the global scope into the function's scope using the global keyword: php:<? global $html_title; ?> You can also use the magical $GLOBALS array: php:<? $GLOBALS['html_title']; ?>
|
# ? Sep 17, 2010 04:15 |
|
Declare that $html_title is global inside the function.php:<? $html_title = "Title of Webpage"; function printTitle(){ global $html_title; echo "Welcome to $html_title"; } ?> For content: Is it a horribly bad idea to use short tags? I've been writing an inhouse app that makes heavy use of <?=$foo;?> as opposed to <?php echo $foo; ?>. I'm not super concerned with portability (its an in-house app) but more concerned with them removing the short tags in a later version. Maniaman fucked around with this message at 04:18 on Sep 17, 2010 |
# ? Sep 17, 2010 04:16 |
Iron Squid posted:Err, actually I wrote my code incorrectly. The code looks more like this: PHP: Variable scope should tell you everything you need to know. The first block of code on that page is basically what I did. The second block of code on that page is what you are trying to do. The third block of code has your answer. Hopefully this code isn't exactly what you are trying to do, just an example, but just in case...What's the point of having a function like that though? The purpose of a function is for code that you want to reuse in other places. Having to declare $html_title outside of the function makes it pretty much useless other places, you would just have to remember what variables you have to declare before you call a function. Something like this makes more sense: code:
code:
|
|
# ? Sep 17, 2010 04:22 |
|
|
# ? May 31, 2024 05:21 |
|
Maniaman posted:Is it a horribly bad idea to use short tags? I've been writing an inhouse app that makes heavy use of <?=$foo;?> as opposed to <?php echo $foo; ?>. I'm not super concerned with portability (its an in-house app) but more concerned with them removing the short tags in a later version. Short tags are in no danger of deprecation. ASP tags are far more likely to find themselves on the chopping block, but are safe for now. Even then, a simple global search/replace will fix things right up. "<?=" is just a shortcut for "<?php echo"
|
# ? Sep 17, 2010 04:56 |