Thread: Valid Class?

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

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Valid Class?


    Does this look like a valid class? I mean if it were to be a part of a program, does it look ok?

    class pSelector {

    Public $pType;
    $error = 'Please input a price.';

    if(this->price > 2000) {

    $pType = 'House';
    echo(this->$pType);

    }

    else if(this->price < 2001) {

    $pType = 'Apartment';
    echo(this->$pType);
    }

    else {

    echo($error);

    }
    }
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    No, you can't have code in the middle of a class, it needs to be inside a function.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Thanks, this better?

    class pSelector {

    Public $pType;
    $error = 'Please input a price.';

    function $pselect(){
    if(this->price > 2000) {

    $pType = 'House';
    echo(this->$pType);

    }

    else if(this->price < 2001) {

    $pType = 'Apartment';
    echo(this->$pType);
    }

    else {

    echo($error);

    }
    }

    $pselect();
    }
  6. #4
  7. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,136
    Rep Power
    1990
    Closer but still wrong.
    1. Function names do not use a $ in front, only variables.
    2. this->price should have a $ in front - $this->price
    3. $this->price actually needs to be declared. It may work depending on your error settings.
    4. All internal class variables must be referenced by $this-> so you can't use $error as you have there, it should be $this->error
    5. Again... You can't have code outside of functions, so your call to $pselect() is not valid (and remember to remove the $ in front of the function name).
    6. Also, internal function names are referenced using $this-> as well, so it would be $this->pselect();

    That's all that I can see for now.

    Oh, and you should use PHP tags to wrap your code in when you paste in on here. That way it will keep the indenting and make it a lot more readable.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Thanks, ok - the class is is set to figure the type of rental it is likely to be based on price. $obj->price = '2000'; is set earlier on in the code. I appreciate the help.

    PHP Code:
    class pSelector {
        
        Public 
    $pType;
        
    $error 'Please input a price.';
        
        function 
    pselect(){
            if(
    $this->price 2000) {
            
                
    $pType 'House';
                echo(
    $this->$pType);
            
            }    
        
            else if(
    $this->price 2001) {
                
                
    $pType 'Apartment';
                echo(
    $this->$pType);
            }
        
            else {
                
                echo(
    $error);
            
                }
            }
    }
    $this-> pselect(); 
  10. #6
  11. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,136
    Rep Power
    1990
    Ok, what you've still missed:
    1. You never actually create an object so you can't call any of it's methods.
    2. You're still using $error when it should be $this->error
    3. You're still using $this->$ptype when it should be $this->ptype
    4. You never set up $price anywhere.
      e
    5. elseif() should be a single word, no spaces. It still works as else if() but that's not the current way to do it.


    Are you working on an editor with syntax highlighting and code "inteligence"? I don't believe so because if you were 99% of the problems that you're going through now would be shown straight away. I'd suggest looking at an editor that can do this for you. My editor of choice is Komodo Edit (the free version) and it does all of this for you.

    As a valid example of what you are looking for:

    PHP Code:
    class pSelector 
         
        public 
    $pType
        public 
    $error 'Please input a price.';
        public 
    $price 0;
         
        function 
    pselect(){ 
            if(
    $this->price 2000) { 
             
                
    $this->pType 'House'
                echo(
    $this->pType); 
             
            }     
         
            elseif(
    $this->price 2001) { 
                 
                
    $pType 'Apartment'
                echo(
    $this->pType); 
            } 
         
            else { 
                 
                echo(
    $this->error); 
            
            } 
        } 
    }

    $p_select = new pSelector ();
    $p_select->pselect (); 
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi DAR1,

    I think it might be a good idea to take some time and actually learn the syntax and logic of object orientation in PHP.

    I mean, sure, you can probably put together some classes through trial and error and help from others. But you'll have much better results and be much faster if you actually know what you're doing.

    There's even an OOP tutorial at php.net.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Do I need to define $price still?

    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);

    echo 
    'Name :: '$obj->namePHP_EOL;
    echo 
    'Price :: $'$obj ['price'], PHP_EOL;
    echo 
    'Address :: '$obj->address_primary' '$obj->getAddressSecondary (), PHP_EOL;
    echo 
    'City, State, Zip :: '$obj->city', '$obj ['state'], ' '$obj->getZip ();

    class 
    pSelector {  
          
        public 
    $pType;  
        public 
    $error 'Please input a price.'
        public 
    $price 0
          
        function 
    pselect(){  
            if(
    $this->price 2000) {  
              
                
    $this->pType 'House';  
                echo(
    $this->pType);  
              
            }      
          
            elseif(
    $this->price 2001) {  
                  
                
    $pType 'Apartment';  
                echo(
    $this->pType);  
            }  
          
            else {  
                  
                echo(
    $this->error);  
             
            }  
        }  


    $p_select = new pSelector (); 
    $p_select->pselect (); 
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Trust me Jacquez, in 5 min at this forum I've learned more than if I were to read a long *** tutorial. I learn differently.
  18. #10
  19. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,297
    Rep Power
    9400
    Fair enough, there are some type of people who learn better that way. But are you learning or are you following directions? Do you understand what not just what they're telling you but why things are the way they are?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by requinix
    Fair enough, there are some type of people who learn better that way. But are you learning or are you following directions? Do you understand what not just what they're telling you but why things are the way they are?
    Yes, I learned that the object identifier $this-> should be used to identify which object we are utilizing. I learned that classes can utilize functions and are not to be mistaken with functions. I learned that else if and elseif are used differently.

    I am still curious about whether I need to define $price since is referenced as $obj->price and set. I do not want to change the price.
  22. #12
  23. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,136
    Rep Power
    1990
    Originally Posted by DAR1
    I am still curious about whether I need to define $price since is referenced as $obj->price and set. I do not want to change the price.
    In the example that you gave above, $price is set in the Property object type, so this means that the pSelect object doesn't need $price to be set.

    BUT... How does the pSelect object know what the price is when it doesn't have any reference to the Property object where the price is actually set? You need to pas in the Property object to the pSelect object somewhere so that it knows what it's supposed to be looking at. As a suggestion...
    PHP Code:
    $p_select->pselect ($obj); 
    Then in the pSelect object...
    PHP Code:
    class pSelector {   
           
        public 
    $pType;   
        public 
    $error 'Please input a price.';  
        public 
    $price 0;  
           
        function 
    pselect($property){   
            if(
    $property->price 2000) {   
               
                
    $this->pType 'House';   
                echo(
    $this->pType);   
               
            }       
           
            elseif(
    $property->price 2001) {   
                   
                
    $pType 'Apartment';   
                echo(
    $this->pType);   
            }   
           
            else {   
                   
                echo(
    $this->error);   
              
            }   
        }   

  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by Catacaustic
    In the example that you gave above, $price is set in the Property object type, so this means that the pSelect object doesn't need $price to be set.

    BUT... How does the pSelect object know what the price is when it doesn't have any reference to the Property object where the price is actually set? You need to pas in the Property object to the pSelect object somewhere so that it knows what it's supposed to be looking at. As a suggestion...
    PHP Code:
    $p_select->pselect ($obj); 
    Then in the pSelect object...
    PHP Code:
    class pSelector {   
           
        public 
    $pType;   
        public 
    $error 'Please input a price.';  
        public 
    $price 0;  
           
        function 
    pselect($property){   
            if(
    $property->price 2000) {   
               
                
    $this->pType 'House';   
                echo(
    $this->pType);   
               
            }       
           
            elseif(
    $property->price 2001) {   
                   
                
    $pType 'Apartment';   
                echo(
    $this->pType);   
            }   
           
            else {   
                   
                echo(
    $this->error);   
              
            }   
        }   

    Thanks, I appreciate the help. I understand now. I redid the class here, if you don't mind taking a look and letting me know what you think. I am curious to know what you think about the way I structured the class and constructor.

IMN logo majestic logo threadwatch logo seochat tools logo