Jump to content


Member Since 06 May 2003
Offline Last Active Nov 23 2015 06:45 PM

#1526215 Controller or Model

Posted by gizmola on 11 November 2015 - 07:43 PM

It really depends on the capabilities of the Model system you are using.  If it was for example, something like Symfony2 and Doctrine2, the typical answer would be that the validation rules are attached to the model.  Of course with that said, form processing rules can get quite complicated, and since the form object lives and dies inside the controller, you will typically have the actual validation check occurring specifically in the controller logic.



Something like:


if ($form->validate()) {
    // Persist the data
    // Redirect as desired
} else {
  //redirect back to form, adding the error data

#1526211 PHP with Java | C/C++ | Python

Posted by gizmola on 11 November 2015 - 07:37 PM

I'm not going to go into the "which is best" question.  The answer always depends on a lot of variables that are typically unique to the circumstance.  Obviously c++ is often used for compiled software, and java is often used due to it's availability on a platform (Android for example) or in the enterprise where one or more application servers are desired.  


In my experience, for server side web development, Python, Ruby, PHP, Java and Node.js are all popular choices that typically come down to the preference of the Lead developer.


In terms of intermixing languages, specifically with PHP one popular way to do that is to use Gearman.  For example, I worked on a project where there was a computation engine written in Java.  The website MVC and additional computation and presentation code was created in PHP using a popular PHP framework.  PHP utilized gearman to send data to the Java computation processes as needed (this had to do with crunching large amounts of historical stock price information) and received the results back for presentation within the PHP framework.  


You can also build your own queueing sub/pub applications using many different technologies, and achieve similar separation of work.  This type of architecture is frequently used where scalability is a significant concern.  An example might be a system like Youtube, where the video encoding or post processing is going to be separated from the front end, and clustered. The clustering and DevOps scaling will be separated from the web application code, and since processing of that type is cpu and IO intensive, you won't have that code running on the same server(s) where the PHP code resides.

#1522147 Open sourcing a website, good idea or?

Posted by gizmola on 02 October 2015 - 07:15 PM

Thank you guys SO MUCH for your help and for being so nice to explain the Git stuff ! Unfortunately my hosting doesn't support Git, however I will start to study it ( for now I only know as much as to publish and commit repositories in GitHub ) and I will try to kick FTP and use Version Control.
Thank you again to all of you!

Start by installing on your workstation and using git for your development work. You can also set up a free account on bitbucket, that will allow you to make private repositories. You can utilize it as a free backup.

-Develop locally, git commit your code each time you have a working build
-Git push to your bitbucket remote

Doing this will get you started and familiar with git as well as giving you the benefit of having change control of your projects. Many a person has accidentally deleted or overwritten an important file. Git will bail you out on those mistakes. It also helps you diff so you can see what you changed revision to revision.

#1520348 Open sourcing a website, good idea or?

Posted by gizmola on 06 September 2015 - 03:49 AM

Git is one of the most important software development technologies to emerge in the last decade. It's pervasive use has given rise to github, and it has basically taken over version control, as well as vastly improving the effectiveness of open source projects.

Yes version control is indispensable in my opinion, not to mention something that differentiates professional developers from hobbyists.

It's also an excellent tool for pushing code to production, especially for small companies.

Old way:

Develop (locally perhaps?)
Figure out manually how many scripts were changed.
Use some tool to FTP files. Do you trust file stamps? Maybe use ftp tool, and pray that you don't have a burp in the middle.
Oh crap! It's not working, did we miss a file? Sit down and try and get it working again by manually going through the list of files trying to remember what exactly you changed.

With Git:

Develop, iterate, push to git branch. For simplicity/1 person you can just use master branch.
Time to deploy? -> git pull (on production machine). Did something go wrong? Git is atomic -- all files will be deployed or none.
Something wrong? git checkout {previous commit#} We are back to where we were, and you can relax and figure out what went wrong on dev!

And of course version control answers questions for you along the way like-- what did I change last month in that module? Did that other programmer add a regression, even though he said he didn't change anything? What is the change history of this file?

Just an amazing tool, from Linus Torvalds, that when all is said and done, will probably be more important to the future of software engineering than Linux.

#1520199 Open sourcing a website, good idea or?

Posted by gizmola on 03 September 2015 - 04:51 PM

For those employers putting weight on things like this, you get more credit for contributing code to other projects.

Don't get me wrong, if you want to be good at something like using git for VC, you have to actually use it, so you could make a bitbucket account and store your full site code in a private repo, and I'd suggest that you do so, but not for the benefit of potential employers.

#1519292 Refactoring Code: does the concept of refactoring exist purely because human...

Posted by gizmola on 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.  

#1519215 i must be missing something...

Posted by gizmola on 18 August 2015 - 10:18 PM

Don't concern yourself with closing the database connection, especially when using MySql.

When the script is done executing all handles are closed for you.

There are very few situations where you need to worry about opening and closing individual database connections within a script.

#1519020 Implementing captcha to my dynamically generated forms

Posted by gizmola on 16 August 2015 - 07:16 PM

In your function comment_form($id, $captcha), you added the $captcha as a param but you never use it in function.

Probably after this line you would use it:

<input type='hidden' name='blog_id' value='$id'>
<input type='submit' name='submit' id='post' value='post'>

#1519015 Implementing captcha to my dynamically generated forms

Posted by gizmola on 16 August 2015 - 06:49 PM

Heredocs will interpolate variables.

So something like this should work:

function comment_form($id, $captcha) {
    global $user_data;
    if (logged_in() === true) {
        return <<<EOT
        <form method='post' action='' class='comments_form'>
            <input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'>
            <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>
            <input type='hidden' name='blog_id' value='$id'>
            <input type='submit' name='submit' id='post' value='post'>
        <hr class='artline'>

$captcha = create_captcha();
echo comment_form($id, $captcha)

Of course you need to alter the create_captcha function so that it returns a string rather than echoing out the markup directly, but that is a general pattern you should be using in all your functions. It will start to improve the separation of concerns.

#1518974 Reverse words in a sentence without using PHP functions

Posted by gizmola on 15 August 2015 - 08:31 PM

The key to requinix's ingenious solution is the use of the '@' error suppression operator.

for ($i = 0; @$input[$i] != ""; $i++) {
This allows him to for-loop character by character through the input by treating the string as a character array, and then by reading beyond the end of the string. That allows him to avoid the use of the strlen() built-in used by Scootsah.

From there it's just concatenating the strings he finds in reverse order. Nice!

With that said, I think what they wanted you to do was use a stack, given the way the question was posed. An array works pretty well for this, so Scootsah's solution is basically what I think they expected.

He reads the words and adds them to the array using "$array[] = $word". You get a numerically ordered "stack".

Then it's just a matter of for looping through the array in reverse order.

So just for fun, here's a way you could walk through the array in reverse order without using a negative for loop to duplicate the array into a reversed form, based on Scootsah's solution:

function reverseString($input)
    $currentWord = '';
    $words = array();

    for ($i = 0; $i < strlen($input); $i++) {
        if ($input[$i] == ' ') {
            $words[] = $currentWord;
            $currentWord = '';

        $currentWord .= $input[$i];

        if ($i == (strlen($input) - 1)) {
            $words[] = $currentWord;

    if (!empty($words)) {
        $output = '';

	for (end($words); key($words) !== null; prev($words)) {
  		$output .= current($words) . ' ';
        return trim($output);

$input = 'this is phpfreaks';
$output = reverseString($input);

echo "Input: $input<br />Output: $output";

#1518965 Greetings!

Posted by gizmola on 15 August 2015 - 07:13 PM

All of the above.

In general terms, the most important thing is to make sites.

You will learn more by doing than all the other methods combined.

#1515933 Replace Characters Algorithm with O(n) Time/Complexity Notation

Posted by gizmola on 09 July 2015 - 01:13 AM

Seems like the mystery is solved.  Nice thread guys!

#1514157 Empty $_POST on form submission?

Posted by gizmola on 17 June 2015 - 08:38 AM

In the future, the fact that you were doing this as a wordpress customization would have been good to know.

#1509180 how to make my website fast ?

Posted by gizmola on 16 April 2015 - 04:45 PM

Neil, you left out:


  • Make a sandwich
  • Take power nap
  • $$$ Profit!

#1508590 scandir()

Posted by gizmola on 09 April 2015 - 04:52 PM

Thanks Barand -- I should have said "practically".  


Also more good reasons why people use echo instead.