Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
can't output my class's variable
Old 12-23-2005, 09:51 AM can't output my class's variable
Junior Talker

Posts: 3
Trades: 0
msgsys class include file:
PHP Code:
class msgsys {
 var 
$message;
 
 function 
addmsg($msg) {
  
$message .= $msg;
  echo 
$message;
 } 
 function 
showmsg() {
  echo 
$message;
 }

formchk class include code:
PHP Code:
class formchk {
 var 
$highlight = array();
 
 function 
validchk($regval$fieldval$msg) {
  if ( !
preg_match($regval$fieldval) ) { msgsys::addmsg($msg); }
 }
 function 
getregex() {
  foreach (
$_POST as $field => $value) {
   switch (
$field) {
    case 
"fname":
     
$regval "/^[a-zA-Z\s]{2,}$/";
$msg '<font size="2" color="#FF0000">First name must only contain alpha(a-z) characters.</font><br><br>';
     
$this->validchk($regval$value$msg);
     break;
   }
  }   
 }

addaccount file code:
PHP Code:
<?PHP
   
if ( $_POST['submit'] == "Add" ) {
    include (
'msgsys_cls.php');
    include (
'formchk_cls.php');
   
    
$srtmsg = new msgsys;
    
$srtchk = new formchk;
    
    
$srtchk->getregex();
   }
  
?>
  <form action = "" method="post">
   <table border="1" width="550px">
    <tr>
      <td colspan="2">
       <?php if ( $_POST['submit'] == "Add" ) { echo 'Echoing out the messege!'$srtmsg->showmsg(); } ?>
      </td>
To start off, when the add buttom is clicked my both classes are being instantiating next, getregex() function from the formchk class is executed. Within that function, I'm passing a message over to the msgsys class by executing this syntax: msgsys::addmsg($msg); Since I'm using scope (: I don't need to use $this-> (reason being I'm not instantiating this class) inside the addmsg function in the msgsys class. To double check that, I used $this-> and my script didnt work correctly, when I didn't use $this-> the script semi worked.

The problem I'm running into is this with: <?php if ( $_POST['submit'] == "Add" ) { echo 'Echoing out the messege!'; $srtmsg->showmsg(); } ?> which is in the addaccount page. $startmsg->showmsg(); is not outputting my message variable. I'm not sure If the variable is losing it's value or what?

Can anyone give me a hand with this?

-Thanks
dottedquad is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-23-2005, 11:18 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
You say you don't instantiate the class, and yet you create one in your addaccount code - if you've set the message with msgsys::addmsg, I would guess you would also need to call showmsg with the same syntax rather than go through the $srtmsg instance.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 12-23-2005, 11:59 AM
Junior Talker

Posts: 3
Trades: 0
Quote:
Originally Posted by 0beron
You say you don't instantiate the class, and yet you create one in your addaccount code - if you've set the message with msgsys::addmsg, I would guess you would also need to call showmsg with the same syntax rather than go through the $srtmsg instance.
Your right I did instantiate the class in my addaccount code to access that class's message variable. I didn't relize that wasn't needed if I accessed it using the Scope Resolution Operator.

So, I changed $srtmsg->message to echo msgsys::showmsg(); and it's still not outputting the variable. I put an echo inside the showmsg function to show the execution of the function which worked. I then also put an echo to output the message value everytime the addmsg function is called and that successfully outputted the message variable. I'm now guessing the message variable is losing is value when it reaches: <?php if ( $_POST['submit'] == "Add" ) { echo 'Echoing out the messege!'; echo msgsys::showmsg(); } ?>

I'm stuck and can't do anything else till this gets fixed. More help is greatly appreciated.
dottedquad is offline
Reply With Quote
View Public Profile
 
Old 12-23-2005, 02:17 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
You shouldn't need the extra echo before msgsys::showmsg(); The function does the echoing itself, you don't need to put echo in front of it. Just
PHP Code:
msgsys::showmsg(); 
should call the function for you.
I'm not an OOP expert in PHP (although I am very familiar with C++, JAVA and python). I'm not quite sure how PHP handles 'static' methods (ie methods that are for the class as a whole, not just particular instances).
What version of PHP are you using? PHP 5 had a variety of changes to the way it handled objects.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 12-23-2005, 09:12 PM
Junior Talker

Posts: 3
Trades: 0
Quote:
Originally Posted by 0beron
You shouldn't need the extra echo before msgsys::showmsg(); The function does the echoing itself, you don't need to put echo in front of it. Just
PHP Code:
msgsys::showmsg(); 
should call the function for you.
I'm not an OOP expert in PHP (although I am very familiar with C++, JAVA and python). I'm not quite sure how PHP handles 'static' methods (ie methods that are for the class as a whole, not just particular instances).
What version of PHP are you using? PHP 5 had a variety of changes to the way it handled objects.
You're right I don't need the echo. I didn't delete the syntax all the way when I changed the code over. When showmsg() runs i have it out putting 'started' to see if it works and it does but, still it's not out putting the message. Would this be because, I'm running the same class again a different time when the message var didn't get set yet?
dottedquad is offline
Reply With Quote
View Public Profile
 
Old 12-24-2005, 08:56 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Okay, I've done some more hunting, and it seems that PHP 5 implements objects in a manner much closer to JAVA/C++ etc than PHP 4 did. If you are using PHP 5 you should be able to do this:
PHP Code:
<?php
class OtherClass
{
   public static 
$my_static 'static var';

   public static function 
doubleColon() {
       echo 
self::$my_static "\n";
   }
}

OtherClass::doubleColon();
?>
^ That's cut and pasted from the php manual on scope resolution.

If you are using PHP 4, it doesn't implement class variables at all, so you have to fake it out somehow - there are several examples in the user comments on this page in the manual.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to can't output my class's variable
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.36257 seconds with 12 queries