Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: OOP Usage

  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    Latvia
    Posts
    65
    Thanks
    8
    Thanked 2 Times in 2 Posts

    OOP Usage

    Hello guys. Just passed my first year of working as a web-site programmer (Self Learned). It is time for me to upgrade, and now I will start to write in OOP Only.

    Here is my first try to use OOP PHP Classes. Now the classes have just basic function, later I will add all the necessary functions. Now what I want is - Can you guys check this out, and give me suggestions how to do this better.. Since I am learning OOP just by google, I am sure I am not doing this as it should be..

    PHP Code:
    <? 

    # EXCHANGE CLASS

    class exchange {

        public 
    $method;

            function 
    rewrite_mehod_name_full() {
          
                global 
    $global;

                    
    $method $this -> method
                    
    $method $global['global']['payMethod_full'][$method];
                    return 
    $method
            }
            
            function 
    rewrite_mehod_name_short() {
          
                global 
    $global;

                    
    $method $this -> method
                    
    $method $global['global']['payMethod_short'][$method];
                    return 
    $method
            }    
         
            function 
    rewrite_mehod_currency() {
          
                global 
    $global;

                    
    $method $this -> method
                    
    $currency $global['global']['payMethod_currency'][$method];
                    return 
    $currency
            }


    // Define the Object alrady here, and include this in files where its necessary

    $objekts = new exchange;
    $objekts2 = new exchange;


        
    // On other pages I use Objects like this ..

            
    $objekts -> method $bill['from_method'];
            
    $objekts2 -> method $bill['to_method'];
            echo 
    $objekts -> rewrite_mehod_name_short();

            
    # BILLS CLASS

    class bill {

        public 
    $id;
         
            function 
    select_bill() {
        
                
    $bill mysql_fetch_array(mysql_query("SELECT * FROM `bills` WHERE `id`='".$this -> id."'"));
                return 
    $bill;
            }
            
            function 
    show_description() {

                global 
    $website;
                
                
    $data $this -> select_bill();
                
                
    $obj_from = new exchange;
                
    $obj_to = new exchange;            
                
    $obj_from -> method $data['from_method'];
                
    $obj_to -> method $data['to_method'];

                
    # WMX - BANK

                
    if(exchange_type($data['from_method'], $data['to_method']) == 1) {
                
                    return 
    'NR: ' .$data['nr'] . ' Withdrawal: ' $data['BANK_IBAN'] . ' 'format_2($data['from_amount']) . ' '$obj_from -> rewrite_mehod_name_short() . ' Total: 'format_2($data['to_amount']) . ' '$obj_to -> rewrite_mehod_currency() .  ' Fee: 'format_2($data['my_cut']) . ' '$obj_to -> rewrite_mehod_currency() .  ' | '$website['domain'];
                }
                
                
    # WMX - WMX

                
    if(exchange_type($data['from_method'], $data['to_method']) == 2) {
                
                    return 
    'NR: ' .$data['nr'] . ' 'format_2($data['from_amount']) . ' '$obj_from -> rewrite_mehod_name_short() . ' -> 'format_2($data['to_amount']) . ' '$obj_to -> rewrite_mehod_currency() .  ' | '$website['domain']; 
                }
                
                
    # BANK -> WMX

                
    if(exchange_type($data['from_method'], $data['to_method']) == 3) {
                
                    return 
    'NR: ' .$data['nr'] . ' 'format_2($data['from_amount']) . ' '$obj_from -> rewrite_mehod_currency() . ' -> 'format_2($data['to_amount']) . ' '$obj_to -> rewrite_mehod_name_short(); 
                }        
                
                
    # WN - WMX

                
    if(exchange_type($data['from_method'], $data['to_method']) == 4) {
                
                    return 
    'NR: ' .$data['nr'] . ' 'format_2($data['from_amount']) . ' '$obj_from -> rewrite_mehod_currency() . ' -> 'format_2($data['to_amount']) . ' '$obj_to -> rewrite_mehod_name_short() .  ' | '$website['domain']; 
                }            
                
            }
            
    }

    // Make Object

    $obj_bill = new bill;

    // This is how I use this on the necessary pages

    $obj_bill -> id 286;
    echo 
    $obj_bill -> show_description();

    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Drop your use of global; it creates a debugging nightmare when it can be altered directly from any scope. Pass this value as a parameter either during construction or through method calls themselves.

    PHP is datatype weak. You should never expose a property as being public since you cannot control what data goes into it. Always use accessors and mutators.


  •  

    Posting Permissions

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