Page 1 of 1
a->x->valid, a->y->valid function in class?
Posted: Mon Jul 11, 2011 3:31 am
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!
Re: a->x->valid, a->y->valid function in class?
Posted: Mon Jul 11, 2011 5:12 am
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);
Re: a->x->valid, a->y->valid function in class?
Posted: Mon Jul 11, 2011 6:10 am
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
Re: a->x->valid, a->y->valid function in class?
Posted: Mon Jul 11, 2011 4:40 pm
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
Re: a->x->valid, a->y->valid function in class?
Posted: Tue Jul 12, 2011 1:42 am
by social_experiment
McInfo wrote:Method chaining can do some silly stuff...
Are there any instances where it is useful
Re: a->x->valid, a->y->valid function in class?
Posted: Tue Jul 12, 2011 12:13 pm
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);
Re: a->x->valid, a->y->valid function in class?
Posted: Tue Jul 12, 2011 1:05 pm
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?
Re: a->x->valid, a->y->valid function in class?
Posted: Tue Jul 12, 2011 3:13 pm
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.)
Re: a->x->valid, a->y->valid function in class?
Posted: Tue Jul 12, 2011 3:49 pm
by social_experiment
Cool, thanks for the explanation