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!
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:
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...
<?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
<?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
<?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
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.)