Forum Moderators: coopster

Message Too Old, No Replies

unsure what :: is doing in ref to a class file in a condition

         

php4U

6:36 pm on May 11, 2009 (gmt 0)

10+ Year Member



I have a set of checks in a form like the following but I'm trying to implement another check which uses a class file...
basic if only checks echo the errors
$e = array(); // An empty array to hold possible error messages
if ( $email=='' ) {
$e[] = 'Please enter the email address of the person you\'re sending the message to<br>';
}
if ( empty($e) ) { // the error array is empty
//...processing here }

This is the code I'm having an issue with...

/* first we need to require our MathGuard class */
require ("ClassMathGuard.php");
if (MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code'])) {
echo ("Correct answer!");
} else {
$e[] = 'Incorrect answer, please try again!<br>';
}

I did some looking but couldn't find out what the :: is doing before checkResult

I basically need to eliminate echo'ing Correct answer! and only show the error. I tried to use ! marks in front of $_REQUEST but that didn't seem to work. The class file isn't very long, let me know if you need to see it and I will post it.

Can anyone help me out? Thank you

AlexK

7:25 pm on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



`::' is used for a class function capable of being used without a class instance.

So:

require(a_class.php);

1 Class + class functions are now available to be referenced.

$a = new a_class;

2 Class has now been instantiated (an instance of the class has been created).

At both (1) + (2) the function `a_class::a_function()' could (possibly) be used (the function must NOT attempt to access any class variables if used in this way).

However, the function `$a->a_function()' can only be used at (2), because this is a reference to the function of a specific instance of the class.

To directly answer your question, you need to invert the result of the entire function, not the $_REQUEST:

if ( ! ( MathGuard :: checkResult())) {
}
...or test on the return value directly:
if (( MathGuard :: checkResult()) === FALSE ) {
}

(one set of brackets almost certainly unnecessary, but causes no harm & affords clarity)

php4U

8:18 pm on May 11, 2009 (gmt 0)

10+ Year Member



Thank you AlexK for the great explanation.

I was sort of on the right track by thinking that I had to use an ! mark but clearly had it in the wrong place...so your example was perfect.

This does the job, but I have one final quick question. Before implementing the math guard check if no errors were present it would jump down to the thank you message and not show the form any longer...now with the math guard code in place it sends the message but still shows the form.

I've commented the code to give a background as to what is going on.


<?php
// start form processing which is located at the top of the page
if (isset($_POST['submit'])) {

$e = array(); // An empty array to hold possible error messages

// Everytime an error occurs, add an error message to the errors array.

if (!ereg("^[a-zA-Z]+$", $name) ) {
$e[] = 'Please enter name of person you\'re sending the message to<br>';
}

if ( $email=='' ) {
$e[] = 'Please enter the email address of the person you\'re sending the message to<br>';
}
if ( $mathguard_answer=='' ) {
$e[] = 'Please enter the answer to the math question<br>';
}
/* require the MathGuard class */
require ("ClassMathGuard.php");

if ( ! ( MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code']))) {
$e[] = 'Bad answer, go back to school!<br>';
}

if ( empty($e) ) {

//If the errors array is empty, there were no errors and we can execute the rest of the script
//...all the code to mail the user or any other form processing needed

} // end empty($e) if

if ($e == NULL) {

$success = true; // everything was correct so jump down to the Thank you message and don't show the form anymore

}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php

if ( isset($success) ) { // Form processing above has completed now the success result is being displayed to the user.

echo '<p>Thank you. Your message has been sent.</p>';

} else { // Form either hasn't been submitted or wasn't successful

if ( !empty($e) ) { // If errors array is not empty echo them

foreach ( $e as $msg ) {

echo $msg;
}
}
?>
<!--- HTML FORM HERE --->
<?php
} //close the final if statement
?>


Since the math guard check is now structured like the other checks I thought this should work as it did before the math guard check, but I could be missing something. Thank you for any additional clarification.

AlexK

8:00 am on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if ($e == NULL) {

Since $e is an array, you are using the wrong test. Use the following:

if ( empty( $e )) {

php4U

3:16 pm on May 14, 2009 (gmt 0)

10+ Year Member



Thank you for taking another look AlexK. I made the change to
if ( empty( $e )) {
and got the same result.

I was however able to get this working in another form that I have used in the past, and it had a little more validation.