Jump to content


Member Since 06 May 2003
Offline Last Active Today, 04:50 AM

Posts I've Made

In Topic: How do you define constants that have been posted from a form?

Today, 04:54 AM


   Agreed 100%.  I stated this, but without the examples you provided, when I said that it would treat them first as constants, generate a notice, but still resolve them as string constant keys in the array.


As you state the manual indicates that you can leave off the single quotes, but I think that's bad form, and as it happens causes the notice to fire.  


If we're trying to be notice -free, then the way to go is to use the blocks around the array variables, and I think that is what people should be taught, and become comfortable with, and adopt as a best practice.

In Topic: How do you define constants that have been posted from a form?

Yesterday, 10:17 PM

These are 'Notices'.  A notice is not an error per-se.


Your code will work without the use of a string index:


name vs. 'name'


However, you are causing PHP to do extra work, because it 


  • generates a notice (if the error level includes notices)
  • has to first check to see if there is a constant with that name


Obviously this is not best practice.   Frequently I find that people have gotten into the bad practice of omitting single quotes around the string constants they use, because they are having parsing problems with interpolation.



This doesn't work:


$name['first'] = 'Fred';
$name['last'] = 'Stone';
$output = "Hi $name['first']!  I hope that you and the rest of the $name['last'] family have a great visit with us at our first class resort!";
echo $output;


Run this and it generates a parsing error:


PHP Parse error:  syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)


The secret to getting around this issue is to wrap the array variables in a php block -  { }.


This code works:


$name['first'] = 'Fred';
$name['last'] = 'Stone';
$output = "Hi {$name['first']}!  I hope that you and the rest of the {$name['last']} family have a great visit with us at our first class resort!";
echo $output;




Hi Fred!  I hope that you and the rest of the Stone family have a great visit with us at our first class resort!

In Topic: Function calls to db not working

Yesterday, 08:29 AM

Global variables introduce side-effect bugs, and go against the grain of good procedural and object oriented coding practices.


A function or method should be discreet and provide unit testable reproducible behavior.


Specific input(s) should produce specific results and return values.


When you use globals, the function stops being discreet, and becomes dependent on the state of the global variables used.  Worse yet, the globals are available and shared by any code that desires to make use of them.  


This leads to situations where you will have a hard time tracing down or reproducing problems because some path of execution has lead to a global variable being changed, and you won't be able to know easily what lead to the problem.


They also contribute in general to spaghetti code where it becomes difficult to trace or understand what is happening.


Specifically, imagine you are looking at some source code and a function is called, that then makes use of a global variable.  


Given the alternative of using a parameter, you can see where the parameter got it's value in the source code, because to call it you have to pass the parameter specifically.


Contrast this with a global, where you may have no idea what has happened to that variable previously, where it came from, or what created its value.  It is simply referenced in the source code at some point.

In Topic: Magic quotes

20 August 2015 - 01:33 AM

My goodness that is some really old code.  


What CMS are we talking here?

function slashString_gpc() {
   foreach (array('REQUEST', 'COOKIE') as $gpc)
   $GLOBALS["_$gpc"] = array_map('slashString', $GLOBALS["_$gpc"]);

Inside there looks like it should be:

function slashString_gpc() {
   foreach (array('REQUEST', 'COOKIE') as $gpc)
   $GLOBALS[$gpc] = array_map('slashString', $GLOBALS[$gpc]);

In Topic: Refactoring Code: does the concept of refactoring exist purely because human...

20 August 2015 - 01:16 AM

Here's what often happens:


You join a company/project that is trying to build some software to do a number of things, as set out in the goals/design/specifications.  


You break things up into tasks and different members of the team start working on them.


Frequently there is a framework being used, and hopefully some conventions, but frequently there is a lot of uncertainty.


People start plowing away, and the way Programmer A does things vs. Programmer B might be substantial.


Now you have 2-3 semi-working pieces of the product and you start looking at the code, and right away it's obvious that this code was produced by two different people.  


As "hypothetically" I might have been one of the people involved, even as I was coding my piece, I realized along the way that there were some things my code did, where given more time I might have made it more reusable, or had a better design, so if I'm lucky I might stashed a few comments or TODO in my code.


Now we're on to more items, and programmer 2 is doing something very similar to the first item, and there's a lot of copy and pasting involved.


We do some code reviewing, and it's clear to everyone that this is not DRY.  There is a lot of code being repeated.


Programmer 3 just joined, and now has to do something very similar to what Programmer 1 did, and I did, but the problem is, that even though they are similar items, the team has not agreed which is the best way to do it, so Programmer 3 basically has to just pick someone to emulate (if we're lucky) or maybe decides that everyone else was an idiot and does the new component in a completely different way than the two previous components.


The PM and QA are reviewing things, and everyone is super excited because we're only three weeks behind the original schedule (of completion in 6 hours) and the system basically seems to do the things that it was supposed to do.


The only problem is, that the underlying code now looks like it was assembled by a chimp with ADD, and there are 12 new features planned for next week....



If you're lucky, maybe there's a lead dev, who goes in and adds a couple of much needed unit tested components, and redoes one of the sections and this then becomes "the reference" for how to do something similar.  


If you're lucky, there's some design patterns being used.


If you're lucky there are code reviews.


If you're lucky people go back before anyone notices and refactor things to be consistent.


If you're lucky you have appropriate roles and responsibilities and some division of labor.


Programmers that care about the overall quality of the underlying design, or are incented to insure it has minimal bugs, or have to maintain it or keep it running are going to be interested in refactoring.


Then there are situations where someone is brought in to start working on legacy code, and they realize that what they've just been assigned to work on is a rat's nest of rotten spaghetti, and those three things that "the old guy never got around to adding but should be easy right?" are actually close to impossible to add given the original architecture (or lack thereof) and the fact that doing just about anything to it has the potential to create side-effects that will break unknown areas of the system, causing everyone to question the new programmer's basic competence.


No, I would not say that refactoring is related to understanding someone else's code, and thus used as an excuse because you can't understand what they were thinking.