Jump to content

Recommended Posts

Hi genius,

 

I am processing a form on the same page using the procedural approach like this:

<?php
if(isset($_POST['submit']))
{
  if(trim($_POST['firstname']==""))
   {
     echo 'firstname is missing';
    }
   else{
     echo 'firstname is fine';
   }
   
}
?>

 

How do I apply such logic using OOP approach.

 

Thanks in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/153345-solved-trying-to-achieve-this-in-oo/
Share on other sites

This is not a case for OOP. However you could create a validation object. You would have to assign the input and the test you wish to run.

 

i.e.

 


class validate {
  private $data;  
  public $errors;

  public function __construct($data) {
    $this->data = $data;
    $this->errors = array();
    $this->processData();
  }

  private function processData() {
     // loop through data and run each test
  }

  private function testNumeric() {

  }

  private function testEmpty() {

  }
}

$data = array(array($_POST['firstname'],'empty'),
                   array($_POST['age'],'numeric'));
$validation = new validate($data);
if(count($validation->errors)) {
  // display errors
}

Bit too small of an example to make a class out?

<?php
class Mon()
{
   /**
   * @param String $str
   * @return String
   */
    function missingOrNot( $str )
    {
        if( trim( $str ) == '' )
        {
             return $str.' is missing';
        }
        else
        {
             return $str.' is fine';
        }
    }
}

if(isset($_POST['submit']))
{
    $monInstance = new Mon();
    echo $Mon->missingOrNot( $_POST['firstname'] );
}
?>

@ All

 

The example was just an understanding of what am trying to do. I have a form of over 30 input and I will be testing all for errors.

 

Lastly, can I include my class file at the top of the form and if Yes, how is the whole process like.

 

Am new to PHP, please bear with me

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.