PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 4th, 2012, 11:11 PM
DAR1 DAR1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 DAR1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
PHP5 - 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);

}
}

Reply With Quote
  #2  
Old December 4th, 2012, 11:41 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,944 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 16 m 54 sec
Reputation Power: 7053
No, you can't have code in the middle of a class, it needs to be inside a function.
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
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

Reply With Quote
  #3  
Old December 5th, 2012, 12:15 AM
DAR1 DAR1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 DAR1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 16 sec
Reputation 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();
}

Reply With Quote
  #4  
Old December 5th, 2012, 12:43 AM
Catacaustic's Avatar
Catacaustic Catacaustic is offline
Code Monkey V. 0.9
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2005
Location: A Land Down Under
Posts: 1,886 Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 15 h 12 m 20 sec
Reputation Power: 1798
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.

Reply With Quote
  #5  
Old December 5th, 2012, 12:53 AM
DAR1 DAR1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 DAR1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 16 sec
Reputation 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(); 

Reply With Quote
  #6  
Old December 5th, 2012, 01:03 AM
Catacaustic's Avatar
Catacaustic Catacaustic is offline
Code Monkey V. 0.9
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2005
Location: A Land Down Under
Posts: 1,886 Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 15 h 12 m 20 sec
Reputation Power: 1798
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 (); 

Reply With Quote
  #7  
Old December 5th, 2012, 01:33 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 21 m 47 sec
Reputation Power: 813
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.

Reply With Quote
  #8  
Old December 5th, 2012, 01:56 AM
DAR1 DAR1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 DAR1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 16 sec
Reputation 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 (); 

Reply With Quote
  #9  
Old December 5th, 2012, 02:08 AM
DAR1 DAR1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 DAR1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 16 sec
Reputation 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.

Reply With Quote
  #10  
Old December 5th, 2012, 02:19 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,714 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 7 h 6 m 6 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to 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?

Reply With Quote
  #11  
Old December 5th, 2012, 02:25 AM
DAR1 DAR1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 DAR1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #12  
Old December 5th, 2012, 04:31 PM
Catacaustic's Avatar
Catacaustic Catacaustic is offline
Code Monkey V. 0.9
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2005
Location: A Land Down Under
Posts: 1,886 Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 15 h 12 m 20 sec
Reputation Power: 1798
Quote:
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);   
          
        }   
    }   


Reply With Quote
  #13  
Old December 6th, 2012, 12:17 AM
DAR1 DAR1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 DAR1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP5 - Valid Class?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap