Reputation:
I am confused with PHP Class. I know how it works but still confused. Need help to solve the problem.
My question is that I want to check some things from database and for that I will need to create at least three to four functions. Then I will check that all previous functions are returning true or not. If all are returning true then sixth function will work or if any single is returning false then seventh function will run.
Script like this,
<?php
class Function{
function one(){
//does database query
if(success){
return true;
}
else {
return false;
}
}
function two(){
//does database query
if(success){
return true;
}
else {
return false;
}
}
function three(){
//does database query
if(success){
return true;
}
else {
return false;
}
}
function four(){
//does database query
if(success){
return true;
}
else {
return false;
}
}
function five(){
//checks what all function are returning
if(all functions are true){
do_sixth_function();
}else{
do_seventh_function();
}
}
function do_sixth_function(){
//show details to user.
}
function do_seventh_function(){
//forward user to somewhere.
}
}
?>
Something like this but problem is that I don't know how to create fifth function that checks all values of each functions.
can I/should I create function like this?
function five(){
//checks what all function are returning
if((one() === true) AND (two() === true) AND (three()() === true) AND (four() === true)){
do_sixth_function();
}else{
do_seventh_function();
}
}
Let me know if anyone can help me. Thanks anyways. :D
Upvotes: 2
Views: 126
Reputation: 1917
$methods = array('one', 'two', 'three', 'four');
$found = false;
foreach ($methods as $v) {
if (!$this->$v()) {
$found = true;
break;
}
}
if(!$found) $this->do_sixth_function();
else $this->do_seventh_function();
Upvotes: 0
Reputation: 285
Or you can try something like this...
function one(){
if(this is true){
function two();
return true;
}
else {
function false_value();
return false;
}
}
function two(){
if(this is true){
function three();
return true;
}
else {
function false_value();
return false;
}
}
function false_value(){
//this function will handle all invalid or false values
}
so you can keep this going on. By this your function will work like loop and will help you to get more accurate data if you want to insert data in your database and it will help you to fetch data too. Means if first condition is false then it will call the function which will handle all false conditions.
Upvotes: 1
Reputation: 14863
You could do something like:
$methods = array('one', 'two', 'three', 'four');
foreach ($methods as $v) {
if (!$this->$v()) { // Or if (!call_user_func(array($this, $v))) {
$this->seven();
break;
}
$this->six();
}
Upvotes: 3