trenccan
trenccan

Reputation: 720

Using classes in PHP

I'm trying to coding my PHP scripts with classes but it is very difficult to learn. The transition from the old style making me trouble. I know why to use classes but my question is. Can I code everything using classes? Is it effective? Or sometimes "the old style" is better.

Ok. I try to be more specific. Can be this code write with classes?

<?php
$error = array("Odpad nebol zapísaný!");
if (isset($_POST["submit"])) {
$note = isset($_POST['note']) ? $_POST['note'] : '';
$rozmer=$oznacenie_pracoviska=$oznacenie_odpadu=$meno=$vaha=$zberne_miesto="";

if (is_numeric($_POST["zakazka1"]) && is_numeric($_POST["zakazka2"]) && is_numeric($_POST["zakazka3"])) {

    function countDigits($str) {
    return preg_match_all( "/[0-9]/", $str );
    }

    if (countDigits($_POST["zakazka1"])==3 && countDigits($_POST["zakazka3"])==2) {
        $zakazka = array($_POST["zakazka1"],$_POST["zakazka2"],$_POST["zakazka3"]);
        $zakazka_cela = implode("/", $zakazka);
    }

    else {  
        $error[]="Zadajte správny počet číslic v zákazke!";
    }

}

else {
   $error[]="Zadajte číslo zákazky!";
}


if (empty($_POST["rozmer"])) {
    $error[]="Zadajte rozmer!";
}

else {
    $rozmer=$_POST["rozmer"];
}

if (empty($_POST["oznacenie_odpadu"])) {
    $error[]="Zadajte označenie odpadu!";
}

else {
    $oznacenie_odpadu=$_POST["oznacenie_odpadu"];
}

 if (empty($_POST["vaha"])) {
    $error[]="Zadajte váhu!";
}

else {
    $output = str_replace(',', '.', $_POST["vaha"]);

    if (is_numeric($output)) {
        $vaha = str_replace('.', ',', $_POST["vaha"]);
    }

    else {
       $error[]="Váha zadaná v zlom formáte!";
    }
}

if (empty($_POST["oznacenie_pracoviska"])) {
    $error[]="Zadajte označenie pracoviska!";
}

else {
    $oznacenie_pracoviska=$_POST["oznacenie_pracoviska"];
}

 if (empty($_POST["meno"])) {
    $error[]="Zadajte meno!";
}

else {
    $meno=$_POST["meno"];
}

if (empty($_POST["zberne_miesto"])) {
    $error[]="Zadajte kód zberného miesta!";
}

else {
    $zberne_miesto=$_POST["zberne_miesto"];
}

}

Upvotes: 0

Views: 53

Answers (1)

Crunch Much
Crunch Much

Reputation: 1527

First of all OOP is not difficult to learn. If your brain starts thinking that it is difficult so you will never be able to learn it properly and you will never enjoy the OOP style. OOP php has a lot of effective features which makes programming easier and more readable. Start learning OOP in php and i am sure you will enjoy it. You can do everything in OOP what you can do in procedural. although you can use classes in procedural style and that is also an awesome way of PHP programming.

Upvotes: 1

Related Questions