problem with accessing my var from a class...

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!

Moderator: General Moderators

Post Reply
dottedquad
Forum Newbie
Posts: 2
Joined: Fri Dec 30, 2005 2:47 am

problem with accessing my var from a class...

Post by dottedquad »

msgsys class include:

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>';
    }
  }
?>
frmchk class include:

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;
        }
      }
    }
  }
?>
Page code:

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>
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:

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 file
Edit:
I fixed the Parse error. I forgot the $ infront of srtmsg. I'm still having the same issue as I did in php4.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I maybe wrong but, the reason might be, msgsys::addmsg($msg); is initiating the class seperatly every time it's accessed.
You're close. Think about it: your performing a static function call. By doing it that way, $this is not a msgsys message, but actually the current formchk. Try var_dump'ing it and you'll see what I mean.

What you will have to do, since you are calling it statically at the moment, is either make the static call operate on the global instance of msgsys, or give frmchk a msgsys object to work with.

If you need a code example, ask. I think the latter option is more OOP. Also, consider not using abbreviations in your class names.
Post Reply