What the fck. I'm beyond frustrated right now. I've been playing with this fcking class the entire day and everything I see on php.net and various sites doesnt seem to work.

I need to set a constant variable in a class.

I've tried

class get_mail {

  global $mailbox, $username, $password;

  $mailbox = "INBOX";
}

and I get error


Parse error: syntax error, unexpected T_GLOBAL, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/public_html/ajaxemail/index.php on line 41

class get_mail {

  var $mailbox = "INBOX";
}

and get error


Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home/public_html/ajaxemail/index.php on line 41

global $mailbox, $username, $password;
class get_mail {

  $mailbox = "INBOX";
}

and I get error


Parse error: syntax error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/otractco/public_html/micromanagersgroup/ajaxemail/index.php on line 43

Setting the global or public variable out of the class doesnt carry into it, and I cant seem to set anything but functions in a class.

I'm new to classes, thinking oop is what I should be focusing on now. But every example I see on how to carry a variable into a class doesn't seem to work. The only solution I have is to carry the variables into each function but that's tedious and I dont want to do that.

What am I doing wrong?

How can I get a variable into my class?

php 4.47

    first. on PHP 4.x, only var is available. But I don't remind of having errors while pre-defining variables, this one were supposed to work

    class get_mail {
    
      var $mailbox = "INBOX";
    } 

    as the returned error message does not seem to have anything to do with this class

    Second. PHP4 is deprecated and you're supposed to be migrating current PHP4 apps to PHP5, not developing new ones.

      Globals are generally not a great idea (they create tightly coupled code which makes it difficult to re-use and can cause maintenance nightmares), and since OOP promotes loose coupling and re-usability, I would recommend something like:

      <?php
      class get_mail 
      {
         var $mailbox;
         var $username;
         var $password;
      
         // constructor:
         function get_mail($mailbox, $username, $password)
         {
            $this->mailbox = $mailbox;
            $this->username = $username;
            $this->password = $password;
         }
      }
      // test it:
      $mailbox = "INBOX";
      $username = "user_name";
      $password = "supersecret";
      $test = new get_mail($mailbox, $username, $password);
      echo "<pre>".print_r($test, 1)."</pre>\n";
      

      If you truly need those 3 variables to be global in scope, such that any change to them in this object would be reflected in any other object that references them, then you probably need to store them in their own class, either as static variables or using the singleton pattern for that class so that any other class's methods can access them.

        Thanks but like is said that doesn't work. I'm not getting any errors but the other functions in the class are showing those variables as null.

        This all looks like a waste of time and I don't see a reason to deal with classes anymore. I dont see any benefit and just looks like more work.

          Thanks but like is said that doesn't work. I'm not getting any errors but the other functions in the class are showing those variables as null.

          Chances are you are using the member variables without the $this-> prefix. As such, they are recognised as local variables instead.

          This all looks like a waste of time and I don't see a reason to deal with classes anymore. I dont see any benefit and just looks like more work.

          It looks like you are just have not understood the syntax. Procedural programming is a valid and well used programming concept, and you can do object oriented (or at least object based) programming without classes, but you should not give up on classes just because you are discouraged by your initial struggle with the syntax.

            You know you could always just use the good old $GLOBALS variable that contains all the global variables if you really can't understand the simple concept of object oriented programming 😛

              If you still need globals (needing globals is a good sign that a rethink is needed - I can't remember the last time I used them), then use the "global" declaration inside the functions that actually use global variables (where those declarations are supposed to have been all along).

                I never use globals but nothing else was working so I tried that last.

                  If what I said does not make sense to you, why not post the current code that you are having trouble with?

                    Write a Reply...