a->x->valid, a->y->valid function in class?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
john1234
Forum Newbie
Posts: 1
Joined: Mon Jul 11, 2011 3:25 am

a->x->valid, a->y->valid function in class?

Post by john1234 »

Hi! :)

I had a function for create user, like cruser inside class and set password like setpw.
I want create validate function to check username and password and I want use like this:

Code: Select all

class abc {
function cruser { }
function setpw {}
??? - need to define here validation or to different class?
}

$a = new class abc();
$a->cruser->validate();
$a->setpw->validate();
How need to define function if I want use like this?
Need new class or other way?
It's so elegant... :)

I'm use PHP 5.2/5.3.

Thanks!
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: a->x->valid, a->y->valid function in class?

Post by phazorRise »

I didn't get what exactly you're asking but perhaps you can use something like -

Code: Select all

class abc{
 public static function validate($data){
   // validation code goes here
 }
}

// call it simply by

$obj=new abc();
$obj->validate($data);

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: a->x->valid, a->y->valid function in class?

Post by social_experiment »

Code: Select all

<?php
class abc {
 protected function _validate($data)
 {
   // validate data
 }

 public function cruser($data)
 {
   $this->_validate($data);
 }

 public function setpw($data)
 {
   $this->_validate($data);
 }
}
 //
 $obj = new abc;
 $obj->cruser($userName);
 $obj->setpw($passWord);
?>
Create a function that validates the data and call it inside each of the functions where you want to validate data
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: a->x->valid, a->y->valid function in class?

Post by McInfo »

Method chaining can do some silly stuff...

Code: Select all

<?php
// This script is not robust.

class UserName {
    var $user;
    var $string;
    function identifies ($user) {
        $this->user = $user;
    }
    function to ($string) {
        $this->string = $string;
        return $this;
    }
    function string () {
        return $this->string;
    }
    function no () {
        return $this->user;
    }
    function object () {
        return $this;
    }
}

class User {
    var $name;
    var $action; // Useless for now
    var $attribute;
    function __construct () {
        $this->name = new UserName();
        $this->name->identifies($this);
    }
    function give () {
        $this->action = 'give';
        return $this;
    }
    function set () {
        $this->action = 'set';
        return $this;
    }
    function your () {
        return $this;
    }
    function name () {
        $this->attribute = 'name';
        return $this->name;
    }
    function wait () {
        sleep(1);
        return $this;
    }
    function it () {
        return $this->{$this->attribute};
    }
}

$user = new User();
$user->set()->your()->name()->to('Joe')->no()->wait()->set()->it()->to('George');
echo $user->give()->your()->name()->string(); // George
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: a->x->valid, a->y->valid function in class?

Post by social_experiment »

McInfo wrote:Method chaining can do some silly stuff...
Are there any instances where it is useful
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: a->x->valid, a->y->valid function in class?

Post by McInfo »

social_experiment wrote:Are there any instances where it is useful
CodeIgniter allows you to build queries like this.

Code: Select all

$this->db->select('name')->from('user')->where('id', $id)->limit(1);
Another use:

Code: Select all

$rectangle->setWidth(300)->setHeight(200);
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: a->x->valid, a->y->valid function in class?

Post by social_experiment »

Code: Select all

<?php
 class ShowName()
 {
  public $number;

  public function setNumber($number)
  {
    $this->number = $number;
  }

  public function showNumber()
  {
    echo $this->number;
  }
 }

 $obj = new ShowName();
 $obj->setNumber(5)->showNumber(); // echo's 5
?>
Ok, is the example above a correct assumption of how method chaining works?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: a->x->valid, a->y->valid function in class?

Post by McInfo »

Whatever is to the left of the -> operator needs to be an object which has a method or property that matches what is to the right of the operator. So, the setNumber() method should return $this. Otherwise, it returns NULL; and because NULL is not an object and does not have a showNumber() method, the script will trigger "Fatal error: Call to a member function showNumber() on a non-object".

(And the class definition header should not have parentheses.)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: a->x->valid, a->y->valid function in class?

Post by social_experiment »

Cool, thanks for the explanation
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply