problem with accessing my var from a class...
Posted: Fri Dec 30, 2005 3:07 am
msgsys class include:
frmchk class include:
Page code:
I ported this code over from php4 to php5 because OOP works a little better in php5 then php4. The problem I had in php4 is:
msgsys::showmsg() is not outputting the message var from the msgsys class. I maybe wrong but, the reason might be, msgsys::addmsg($msg); is initiating the class seperatly every time it's accessed. Therfor, setting the message var inside msgsys class without saving the past values since it's accessed seperatly. Am I correct and How would I fix this issue?
Also, I know the formcheck class is redundent but I'll figure out a new way to code this class once I understand how to access classes from within other class code
The problem I'm having in php5:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /wwwroot/htdocs/ppcart/index.php on line 17
Line 17 is:
Edit:
I fixed the Parse error. I forgot the $ infront of srtmsg. I'm still having the same issue as I did in php4.
Code: Select all
<?php
class msgsys {
//stores the messages
var $message = '';
//adds the msg values to the message var
function addmsg($msg) {
$this->message .= $msg;
}
//when called, will output the message variable.
function showmsg() {
echo 'Showmsg function ran!' . '<br>';
}
}
?>Code: Select all
<?php
class formchk {
var $highlight = array();
//checks the regex agains the field value.
function ValidateCheck($regval, $fieldval, $msg) {
if ( !preg_match($regval, $fieldval) ) { msgsys::addmsg($msg); } //if error then call the addmsg function from the msgsys class include file
echo msgsys::showmsg();
}
function SetCheckValues() {
//loops through the $_POST fields
foreach ($_POST as $field => $value) {
//sets the check values and it's error message to variables
switch ($field) {
case "username":
$regval = '/^[a-zA-Z0-9]{4,20}$/';
$fieldval = $field;
$msg = '<font size="2" color="#FF0000">User name must be a minimum of 4 and a maximum of 10 alpha(a-z) characters long. May also contain numeric characters(0-9).</font><br>';
$this->ValidateCheck($regval, $fieldval, $msg);
break;
case "password1":
$regval = '/^[a-zA-Z0-9]{4,20}$/';
$fieldval = $field;
$msg = '<font size="2" color="#FF0000">Password must have a minimum of 4 and a maximum of 10 alphanumeric(a-z and 1-9) characters with out symbols. Also, both password fields must match.</font><br>';
$this->ValidateCheck($regval, $fieldval, $msg);
break;
case "fname":
$regval = '/^([a-zA-Z\.\-\_\'\s]+){2,}$/';
$fieldval = $field;
$msg = '<font size="2" color="#FF0000">First name must only contain alpha characters.</font><br>';
$this->ValidateCheck($regval, $fieldval, $msg);
break;
case "lname":
$regval = '/^([a-zA-Z\.\-\_\'\s]+){2,}$/';
$fieldval = $field;
$msg = '<font size="2" color="#FF0000">Last name must only contain alpha characters.</font><br>';
$this->ValidateCheck($regval, $fieldval, $msg);
break;
}
}
}
}
?>Code: Select all
<?php
if ( $_POST['submit'] == "Add" ) {
include ('msgsys_cls.php');
include ('frmchk_cls.php');
//intiates both the msygsys class and formchk class to be use
$srtmsg = new msgsys;
$srtchk = new formchk;
$srtchk->SetCheckValues();
}
?>
<form action = "" method="post">
<table alight="center" width="550px" border="1px">
<tr>
<td colspan="2">
<?php if ( $_POST['submit'] == "Add" ) { echo 'Error message:' . "<br>"; srtmsg->showmsg(); } //is not outputing the message var from the msgsys class include file ?>
</td>
</tr>msgsys::showmsg() is not outputting the message var from the msgsys class. I maybe wrong but, the reason might be, msgsys::addmsg($msg); is initiating the class seperatly every time it's accessed. Therfor, setting the message var inside msgsys class without saving the past values since it's accessed seperatly. Am I correct and How would I fix this issue?
Also, I know the formcheck class is redundent but I'll figure out a new way to code this class once I understand how to access classes from within other class code
The problem I'm having in php5:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /wwwroot/htdocs/ppcart/index.php on line 17
Line 17 is:
Code: Select all
<?php if ( $_POST['submit'] == "Add" ) { echo 'Error message:' . "<br>"; srtmsg->showmsg(); } //is not outputing the message var from the msgsys class include fileI fixed the Parse error. I forgot the $ infront of srtmsg. I'm still having the same issue as I did in php4.