gmgj Posted January 29, 2013 Share Posted January 29, 2013 class CSESSION{ //initialize strings public $City = ''; public $State = ''; public $Where = '' .. // Constructor: public function __construct($ip) { $this->IP = $ip; $this->getIPinfo($ip); //constuctors don't return anything } protected function getIPinfo($ip) { // Identify the URL to connect to: $url = 'http://freegeoip.net/csv/'.$ip; // Open the connection: $fp = fopen($url, 'r'); if (!$fp) { echo "Error opening url:".$url." for ip: ".$ip."<BR>"; return false; } --------------------------------------- My contstructor calls a function which returns a value if a connection can't be made But contstructor cannot have a return statement. I thought I would use the class like this // Create it new object:$cs = new CSession($ip);if ($cs->where) {------------------------------------ Can I get a hint how an experienced php oop guy might do this? Link to comment Share on other sites More sharing options...
Antonio Conte Posted January 30, 2013 Share Posted January 30, 2013 Several ways. 1. Cast an exception. This is probably the best solution. If you can't open the connection, that break application crucial functionality. That's worthy of an exception in my mind. Objects that has errors (real errors) should not be created. That's an important principle. 2. Create a standalone setter and force a call to that instead. You already have this, but you change it's modifier to public instead. That way, you can easily check the result of the method. This method should return an exception if errors occur non-the-less, so I suggest number one instead. This solution might be viable if you find ways to possibly find a solution, like checking several URLs and that sort of things. In my mind, this seems like an exception case. If it's not valid, it's an exceptional state and should not work. If it breaks main functionality, always trow exceptions. Link to comment Share on other sites More sharing options...
Jaepee Posted January 30, 2013 Share Posted January 30, 2013 Correct me if I am wrong but spelling is incorrect, shouldn't it be \/ \/ \/ \/ \/ $cs = CSESSION($ip); 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted January 30, 2013 Share Posted January 30, 2013 Edit. Yes, he needs to change either the definition or the initiation. I think the former makes more sense. Link to comment Share on other sites More sharing options...
Jaepee Posted January 30, 2013 Share Posted January 30, 2013 Thank you sir. But, would $cs = new CSESSION($ip); call the same class as $cs = new CSession($ip); I thought that classes were or are case sensitive? I have been coding JavaScript a little to much I forgot. Sorry if my post is off topic. I do find that if I do not code in a certain language for a while BOY do I get Rusty and ofcourse I love coding php. I would love to remember everything that I learn however that seems to not be the case in my small programming life. jp Link to comment Share on other sites More sharing options...
Antonio Conte Posted January 30, 2013 Share Posted January 30, 2013 Ah. I did not pick up on that. I see now that the class definition is in uppercase while the object initiation is camel case. Sorry about that, Jaepee. You are correct. gmgj: You need to change the class definition. Secondly, you should change the visibility on your properties and use setters and getters instead. That's the principle of encapsulation. 1 Link to comment Share on other sites More sharing options...
gmgj Posted February 1, 2013 Author Share Posted February 1, 2013 Thanks , getters, setters on protected attributes is more encapsulated throw an exception UpperCamelCase for class name. Link to comment Share on other sites More sharing options...
Recommended Posts