#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Difference Between these 2 $obj


    $obj ['address_primary'] = '100 Main St';

    $obj->address_secondary = 'STE 100';

    how would I reference the top within a class?

    $this->address_primary ?
  2. #2
  3. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by DAR1
    $obj ['address_primary'] = '100 Main St';

    $obj->address_secondary = 'STE 100';

    how would I reference the top within a class?

    $this->address_primary ?
    as far as I see it (with limited knowledge) the top variable is a array variable with the key of address_primary.
    I guess you refer to it like so:

    PHP Code:
    $this->$obj['address_primary']; 
    edit: Since I am learning OOP myself I thought I post this:
    PHP Code:
    class SomeClass{
        
        public 
    $obj = array('address_primary' => '100 Main St');
        
        function 
    GetAddress(){
            return 
    $this->obj['address_primary'];
            
        }
    }

    $example = new SomeClass;

    echo 
    $example->GetAddress(); 
    Last edited by aeternus; December 5th, 2012 at 06:38 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Valid Code? Appreciate help, learning a lot.


    Does this look like a decent class & constructor? Could it be written better, also do you see any mistakes or referencing that is wrong? I appreciate the feedback. My main concern is the $obj->address_primary.. I am worried I have forgotten how to address this. I haven't coded php in over a yr & need a reminder.


    PHP Code:

    include('Property.php');

    $obj = new Property ();
    $obj->setName ('My Property');
    $obj->price 2500.00;
    $obj ['address_primary'] = '100 Main St';
    $obj->address_secondary 'STE 100';
    $obj->city 'Townsville';
    $obj->state 'VA';
    $obj->setZip (12345);

     class 
    House {
               
               var 
    $name;         // House Name
               
    var $price;   // Price of House
               
    var $address_1;         // Address 1
               
    var $address_2;    // Address 2
               
    var $city// City
               
    var $state// state
               
    var $zip// zip
               
               
               // Class Constructor
               
    function House($name$price$address_1$address_2$city$state$zip) {
                    
    $this->setName $name;
                    
    $this->price $price;
                    
    $this->address_primary $address_1;
                    
    $this->address_secondary $address_2;
                    
    $this->city $city;
                    
    $this->state $state;
                    
    $this->zip $zip;
               }
               
    // Getter/Setter functions
               
    function get_name() {
                    return 
    $this->name;
               }
               function 
    set_name($newname) {
                    
    $this->name $newname;
               }
               function 
    get_price() {
                    return 
    $this->price;
               }
               function 
    set_price($newprice) {
                    
    $this->price $newprice;
               }
               function 
    get_address_1() {
                    return 
    $this->address_primary;
               }
               function 
    set_address_1($newaddress_1) {
                    
    $this->address_primary $newaddress_1;
               }
               function 
    get_address_2() {
                    return 
    $this->address_secondary;
               }
               function 
    set_address_2($newaddress_2) {
                    
    $this->address_secondary $newaddress_2;
               }
               function 
    get_city() {
                    return 
    $this->city;
               }
               function 
    set_city($newcity) {
                    
    $this->city $newcity;
               }
               function 
    get_state() {
                    return 
    $this->state;
               }
               function 
    set_city($newstate) {
                    
    $this->state $newstate;
               }
               function 
    get_zip() {
                    return 
    $this->setZip;
               }
               function 
    set_zip($newzip) {
                    
    $this->setZip $newzip;
               }
               
               
    // Dump Data
               
    function output() {
                    echo 
    "House: ".$this->name."<br>";
                    echo 
    $this->name." is located at ".$this->address_primary.", ".$this->city.", ".$this->state." ".$this->setZip."<br />";
                    echo 
    "Asking Price: $".$this->price;
       
               }
          } 
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Threads merged since they're dealing with the same code and on the same subject of code quality.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by requinix
    Threads merged since they're dealing with the same code and on the same subject of code quality.
    Thank you requinix. Appreciate it.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by aeternus
    as far as I see it (with limited knowledge) the top variable is a array variable with the key of address_primary.
    I guess you refer to it like so:

    PHP Code:
    $this->$obj['address_primary']; 
    edit: Since I am learning OOP myself I thought I post this:
    PHP Code:
    class SomeClass{
        
        public 
    $obj = array('address_primary' => '100 Main St');
        
        function 
    GetAddress(){
            return 
    $this->obj['address_primary'];
            
        }
    }

    $example = new SomeClass;

    echo 
    $example->GetAddress(); 
    Thanks, problem is the first 15 or so lines is fixed - I can't touch it, I am just to write a class for a house based on the information given. So, I can only build the class. So, I am using $this->$obj['primary_address'] = $address_1;

    Not getting any errors, but I am not connected to a server so I can't run php. But the editor seems to think it is alright.

IMN logo majestic logo threadwatch logo seochat tools logo