![]() |
My First Class - Any Good?
Hey,
I've never coded using PHP 5's OOP, and while I was planning a new project, I knew I was going to be using it, as well as using the MySQLi function. I've read loads of stuff now on OOP and I think I've got the hang of it, sorta, but at a newbie stage... ya know? lol :-PI whipped up this code, I just want you to tell me what I did right and wrong and how it can be improved: PHP Code:
|
It is a good start! A few things:
You dont have the class instance variables x and y defined. Code:
$this->x = $x;PHP Code:
PHP Code:
MysqlI rules, doesn't it? :D PHP Code:
|
Quote:
PHP Code:
-m |
Yeah :) I am more about pushing good standards before they start making poor programming standards.
I wish PHP was more strict in this area, heh. |
I'm glad it's not. As I mentioned, and as you said, it is good practice to define properties that you are going to need, and in a lot of cases it's even better practice to make sure those properties are intialized to some degree in your construct or any method that needs them.
That being said, there are situations where using a dynamic property can be an asset. If you want, you can even use the built in magic functions so that any dynamic variables you plan on using will exist inside of a defined property, like so; PHP Code:
-m |
I thought we didn't use
var anymore? I'm using PHP 5. Isn't it:PHP Code:
|
Yeah, var is php4 OOP. With PHP5 you set visibility with the public, private and protected keywords.
-m |
yes using var is deprecated and removed completely in php6 i think.
|
Why are you using self::$localhost and self:: all over the place?
If you're using PHP5, then you have use a static variable, and you aren't, so.. you need to use $this->localhost etc. |
Because you can't access class constants and/or static properties using the $this keyword.
|
Quote:
|
Quote:
Quote:
I'd always used define and remembered once I saw a different way of doing that with OOP, ala const so seeing as I love being different, I did it that way :P |
Quote:
I've seen some really funky stuff going along with inheritance and constants though so I stayed away from them :D |
Sorry for the double post!
Is it good coding to put the MySQL connection inside the __construct function?Also I tried doing: PHP Code:
|
Well, since this is a math class, I would actually have a seperate class for the database as well
PHP Code:
|
Static methods/variables and class constants are bound to the class therefore we must use self to reference the current class, $this however references the current object not class, remember there is a fundamental difference between the two; that is why you can't use $this to access methods/properties marked as static or class constants because they are bound to the class not the object.
|
Hm, weird.. well if I were you I would use private $localhost instead of constants :-P
|
Quote:
I guess my real question is, what is the purpose to making a static method vs public? I understand private, protected, and public, but not static. edit: Declaring class members or methods as static makes them accessible without needing an instantiation of the class. More on visibility: The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item. |
Static methods are awesome. But not really needed in this case drewbee
|
Quote:
PHP Code:
|
| All times are GMT. The time now is 06:47 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0