PHP speed question

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
[)roi(];18179921 said:
FYI Some general best practice:
  1. Make it work + create unit tests.
  2. Make it right (Refactor; adopt a style, code is readable, clear naming convention and remove duplication). Repeat step 1 & 2 until it works
  3. Use a profiler to find performance bottlenecks (only do this at the end, no premature optimization).

Wow thank you for the Unit testing link!

That is something I WILL master, I firmly believe it too, that way you can nip it in the bud before it even starts
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
[)roi(];18179985 said:
Lol... I should have guessed :D

PhpStorm wasn't cheap so I abuse most of the stuff it offers over brackets.io
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
PhpStorm wasn't cheap so I abuse most of the stuff it offers over brackets.io
Personally not a fan of Jetbrains stuff except maybe Resharper; can't talk about PhpStorm, never tried it.
Plus for the little PHP I code these days, I'm content mostly with Sublime Text, and on the rare occasion also in Eclipse.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
First attempt at this whole OOP business.

JS for Ajax.
HTML:
$(document).ready(function () {
    $('#newsletter').submit(function () {
        var $this     = $(this),
            $response = $('#response'),
            $mail     = $('#signup-email'),
            testmail  = /^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/,
            hasError  = false;

        $response.find('p').remove();

        if (!testmail.test($mail.val())) {
            $response.html('<p class="error">Please enter a valid email</p>');
            hasError = true;
        }

        if (hasError === false) {

            $response.find('p').remove();
            $response.addClass('loading');

            $.ajax({
                type: "POST",
                dataType: 'json',
                cache: false,
                url: $this.attr('action'),
                data: $this.serialize()
            }).done(function (data) {
                $response.removeClass('loading');
                $response.html('<p>'+data.message+'</p>');
            }).fail(function() {
                $response.removeClass('loading');
                $response.html('<p>An error occurred, please try again</p>');
            })
        }


        return false;
    });
});

PHP for handling all of it:
PHP:
<?php
class Newsletter
{
    private static $email;
    private static $datetime = null;

    private static $valid = true;

    public function __construct() {
        die('Init function is not allowed');
    }

    public static function register($email) {
        if (!empty($_POST)) {
            self::$email    = $_POST['signup-email'];
            self::$datetime = date('Y-m-d H:i:s');

            if (empty(self::$email)) {
                $status  = "error";
                $message = "The email address field must not be blank";
                self::$valid = false;
            } else if (!filter_var(self::$email, FILTER_VALIDATE_EMAIL)) {
                $status  = "error";
                $message = "You must fill the field with a valid email address";
                self::$valid = false;
            }

            if (self::$valid) {
                $pdo = Database::connect();
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $existingSignup = $pdo->prepare("SELECT COUNT(*) FROM signups WHERE signup_email_address='$email'");
                $existingSignup->execute();
                $data_exists = ($existingSignup->fetchColumn() > 0) ? true : false;

                if (!$data_exists) {
                    $sql = "INSERT INTO signups (signup_email_address, signup_date) VALUES (:email, :datetime)";
                    $q = $pdo->prepare($sql);

                    $q->execute(
                        array(':email' => self::$email, ':datetime' => self::$datetime));

                    if ($q) {
                        $status  = "success";
                        $message = "You have been successfully subscribed";
                    } else {
                        $status  = "error";
                        $message = "An error occurred, please try again";
                    }
                } else {
                    $status  = "error";
                    $message = "This email is already subscribed";
                }
            }

            $data = array(
                'status'  => $status,
                'message' => $message
            );

            echo json_encode($data);

            Database::disconnect();
        }
    }
}
 
Top