October 21, 200718 yr Hey everyone! I need to know if anyone has any really good tutorials out there or know of any good tutorials that teach PHP classes. I am getting pretty decent at PHP but I really can't get past procedure yet. Thank you
October 21, 200718 yr I've been getting into OOP PHP lately myself. I've mostly just used the manual. It explains all aspects of it. If I want to go deeper into a topic I just google it. I also got a book, PHP Bible, which includes just about everything. What PHP version do you use? PHP4 and PHP5 classes have significant differences.
October 21, 200718 yr Like TT, I have lately been trying out my OOP muscles, and to be frank, tutorials are rubbish. Just use the manual to learn the syntax, and write some code. There are a few basic things to remember: to instantiate a class (create a class): class MyClass { } and to create an instance (a unique version of that object): $item = new MyClass; Classes have two types of sub bits, namely "properties" or "methods". Properties are variables belonging to that object, and "methods" are just like functions, but only usable by that class: class MyClass { var $p1; var $p2; function MyFunc() { return $this->p1." ".$this->p2; } } so now something like this (with that code above): $item = new MyClass; $item->p1 = "Richard"; $item->p2 = "Lyon"; print $item->MyFunc(); would print "Richard Lyon". I won't go into things like "why OOP is so useful", because I never understood it until I tried it. SO really, just write some code. This page is useful: http://uk3.php.net/oop
October 21, 200718 yr But keep in mind, if you find yourself using globals inside your classes, you've missed the point of OOP.
October 21, 200718 yr Author Alright, thank you guys. Likewise, I have wanted to start into OOP, I just didn't know where to start. I also have a book on PHP but it's really only like an intro and most of my other PHP knowledge comes from experience. So I'll just play around with it and you the manual. Thank you
October 23, 200718 yr Author Do you guys have any more tips for classes? Also, could you take a look at this code and tell me what I am doing wrong or how to improve on it... It's supposed to take the credentials to the database and then print out a message depending on if it connects or not <?php class database_connect{ var $message = ''; public function _construct(){ $dbc = mysql_connect('localhost','root',''); $dbc2 = @mysql_select_db('blog'); $this->check_connection(); } public function check_connection(){ if(($dbc != 1) AND ($dbc2 != 1)){ $message = 'Fatal Error: Not able to connect to database.'; } else { $message = 'Success!'; } return $message; } } $connect = new database_connect(); $connect->_construct(); print_r($connect); ?>
October 23, 200718 yr I think __construct needs two underscores in front. At any rate you can always just rename it to the name of the class to acheive the same thing. Furthermore, a construct is called automatically when you create the class. The main problem however is the you aren't using the properties correctly. If you want to refer to the properties of the object from inside the object then you need to use $this->varname; Also, you don't really need to seperate your methods into public and private classes. Here is a rewritten bit of code for you: <?php class database_connect{ var $message = ''; var $dbc = FALSE; var $dbc2 = FALSE; public function __construct(){ $this->dbc = mysql_connect('localhost','root',''); $this->dbc2 = @mysql_select_db('blog'); $this->check_connection(); } public function check_connection(){ if(($this->dbc !== 1) AND ($this->dbc2 !== 1)){ $message = 'Fatal Error: Not able to connect to database.'; } else { $message = 'Success!'; } return $message; } } $connect = new database_connect(); print($connect); ?> Note a few other changes: - changed print_r to print. Whilst it will work, print_r is for outputting arrays. Use print (or echo) to output a string. - defined dbc and dbc2 at the start of the class. Any idea for you would be find out how to do this: $connect = new database_connect(); $connect2 = new database_connect("other.server.com","username","password"); print $connect; // localhost print $connect2; // alien server
October 23, 200718 yr - changed print_r to print. Whilst it will work, print_r is for outputting arrays. Use print (or echo) to output a string. Not quite correct. It's for displaying information about a variable in a way that's readable by humans. It's not exclusive to arrays.
October 23, 200718 yr Author Hmm... yeah I will play around try to do it. I know PHP syntax well - don't get me wrong. Just the OOP aspect of it is the hard part for me. I'll learn and I'll try the example you gave for me to do. And about print_r, I used it to try to print the object (which I was doing wrong). I use foreach() for arrays & print() for text and echo() for variables.
October 23, 200718 yr fair enough - i didn't know that about print_r. then again the name makes sense: "print readable"... OOP is a bugger to get a hold of, then once you have you wonder why it was so hard...
October 23, 200718 yr Author Yeah, I'm sure. At first, when I started PHP, I felt like it was going to be too hard but I kept at it and now I like it. I'm sure once I get used to classes, it'll be even better.
October 24, 200718 yr Yes, the step from procedural to OOP can be awkward. Currently my site is a bit of a stitch and mix. I now feel I got a better understanding of it, I even see the use of Interfaces and Abstract classes. (dream poly-morphism) I'm now writing my codebase from scratch in full OOP.
October 27, 200718 yr Author Alright, just a side note, I tried your script PP & it's giving me the same result that I got: Catchable fatal error: Object of class database_connect could not be converted to string in C:\xampp\htdocs\classes\commenting.php on line 21
October 27, 200718 yr Author Alright, I got it. Here's the correct code: <?php class database_connect{ var $message = ''; var $dbc = FALSE; var $dbc2 = FALSE; public function __construct(){ $this->dbc = mysql_connect('localhost','root',''); $this->dbc2 = @mysql_select_db('blog'); $this->check_connection(); } public function check_connection(){ if(($this->dbc !== TRUE) AND ($this->dbc2 !== TRUE)){ $message = 'Fatal Error: Not able to connect to database.'; } else { $message = 'Success!'; } return $message; } } $connect = new database_connect(); $connect = $connect->check_connection(); print($connect); ?> CHANGES: ($this->dbc !== TRUE) AND ($this->dbc2 !== TRUE) instead of them being compared to one - Added $connect = $connect->check_connection();
Create an account or sign in to comment