Jump to content

[SOLVED] Trying to achieve this in OO


depojones

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.