SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    vars in classes ..when ..how ?!

    hi ..

    when and where should i use var in class ?
    i mean this :

    PHP Code:
    class test{
    var 
    text;
    var 
    text2 "";
    .
    .

    is it global ?

    how can u pass vars from class to another class ?

    thx ...

  2. #2
    PHP warrior dkode's Avatar
    Join Date
    Sep 2001
    Location
    Planet Namek
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you actually don't need to use the var before the variable if you don't want to. When i make my classes i dont bother.

    If you wish to use vars from one class in another class thats where inheritance comes in like so:

    PHP Code:
    class myClass1() {
        
    $var1;
        
    $var2 "Some value";

        function 
    myClass1() { // <- Constructor
            // Do some stuff here
        
    }
    }

    class 
    secondClass extends myClass1() {
        
        function 
    get_vars() {
            echo 
    "VAR1 : $var1<br>\n";
            echo 
    "VAR2 : $var2<br>\n";
        }
    }


    // You would call the classes like so:
    $obj = new secondClass;
    $obj->get_vars
    That should return the values of the $var1 and $var2. Theres a really good tutorial on php.net under the class portion of the manual.
    "Mankind cannot define memory, yet it defines mankind"
    -- Project 2501, Ghost in the Shell

    Smarty | PEAR | PHP Manual | MySQL Manual

  3. #3
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think var should only be used as you start a class, and in using it, you shouldn't (as of 4.1.0 I think) assign it a value - you use the constructor for that. Eg.

    PHP Code:
    class Test {
        var 
    text;
        var 
    text2;

        function 
    Test () {
            
    $this->text2 "";
        }

    Variables text and text2 are available to all methods within the class but are not global.

    Not sure about sharing variables between classes, but has extends which allows to effectively stick one class on another. Would like to hear more about that myself, and what you can do with it. Where's FreakySid these days anyway?

  4. #4
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i see, but what if i have config file, where i store such sitename ... etc .. how can i get then to the class, i used global in the function, but some in another post take as strange thing to use global in class !

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    select from MySQL or wherever above either in the function, or out of it and if it's out you have to global it.

    Deos that help?

    Thanks,
    ~someonewhois

  6. #6
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should really not use the global keyword at all in classes. The idea of a class is it's not really "property of your site" - a class should be able to take any local variables from an application and do it's thing, without caring about what the names of those local variables are.

    So when you're declaring methods (functions) within the class, you should be defining the variables that method needs, then passing them from your external scripts, as the method needs them.

    For example;

    PHP Code:
    class MyClass {
        var 
    $myVar;
        function 
    myClass () {
            
    /* Constructor to do whatever */
        
    }

        function 
    mysql_Select ($query$dbconn) {
            
    $sql mysql_query $query$dbconn );
            if ( !
    $sql ) {
                
    $result "Error: " mysql_error ();
            } else {
                
    $result mysql_fetch_array $sql );
            }
            return ( 
    $result );
        }
    }

    $myselect = new MyClass;

    $query "SELECT * FROM table";

    $myresult $myselect->mysql_Select ($query$mysql_connection); 
    That making some sense?
    Last edited by HarryF; Jun 8, 2002 at 06:36.

  7. #7
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    good ..

    but how can u do this with out Global ?

    PHP Code:

    Class test{
    function 
    say($x){
    global 
    $x// with out the result '' !
    $ss $x ;
    }
    }

    // in another file ..
    $e = new test;
    $e->say(hello);

    echo 
    $ss

  8. #8
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I think you'd want to just return $ss from the function, like;

    PHP Code:
    class Test{
        function 
    say($x){
            
    $ss $x ;
            return ( 
    $ss );
        }
    }

    // in another file ..
    $e = new test;
    $ss $e->say(hello);

    echo 
    $ss

  9. #9
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice ..

    and if ...

    PHP Code:
    // Config.php //

    $Site_Name "www.xxx.xxx";

    // in temp.php //

    class test(){
       global 
    $Site_Name ;
            return 
    "The $Site_Name is the best";

    i think here must use the global ?!

  10. #10
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No no - the point I'm making, which other people in Dr Laryy's "PHP Tips" make fanatically thread is never uses global.

    So for that case, I think you want to use the constructor method to "fill in" the variable in the class. E.g.;

    PHP Code:
    <?php
    /* Config.php */
    $site_vars['site_name'] = "www.xxx.xxx";
    $site_vars['site_email'] = "your@yourdomain.com";
    ?>

    <?php
    /* in Test.class.php */

    class Test(){
       var 
    site_name;
       var 
    site_email;
       function 
    Test$array ) {
          
    $this->site_name $array['site_name'];
          
    $this->site_email $array['site_name'];
       }
       function 
    send_mail ($to$sub$mess) {
            
    $mess .= "Visit " $this->site_name " today!\";
            
    $from = "From" . $this->site_email
            if ( !mail ( 
    $to$sub$mess$from ) ) {
                return ( "
    Mail Failed" );
            } else {
                return ( "
    Mail Succeeded!" );
            }
       }
    }
    ?>

    <?php
    /* index.php */
    include ( "
    config.php" );
    include ( "
    Test.class.php" );

    $Testing = new Test( $site_vars );

    $send_mail = $Testing->send_mail ( "them@theirdomain.com" , "Hello", "Just dropping by..." );

    echo ( 
    $send_mail );
    ?>
    Hope that makes some sense and shows some of the tricks you can pull with classes.

  11. #11
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well done.. clear as sun .. thx all ..

  12. #12
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just one thing more ...

    // config.php :

    $Date = date("y-m-d");


    // temp.php :

    class Test {
    function Say($x){
    Global $Date;
    $ss = "$Date<br>".$x
    return ( $ss );
    }
    }

    [/php]

    is that right , i should put as array as u showed above ?

  13. #13
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep - should work no problem. Burn those globals!

  14. #14
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i`ll do ..


    thx dudde !


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •