Hello!
I have some DB full of artists and events for a club.
Database itself is created with lots of relations and now I have in plan to stick with OOP to manage all my data. Since I don't have lots of experience with classes/objects I would like you to help me and correct me if I am making a mistake somewhere...
Ok, first idea is to make a few files which will handle database connection and tables methods. I created a file, let's call it db.inc in which I keep all the information like username/password for db etc.. There's also a function there for connecting to db, something like this;
function db_connect($dbname){
if (!$dbconnect){
$dbconnect = new mysqli($dbhost, $dbusername, $dbuserpass, $dbname);
}
}
After that, I created a file dbClasses which should handle all the queries, methods like getData, addData, editData and similar. That file includes db.inc and in each method i run queries by calling db_connect function, I mean like this;
//some query stuff
$dbconn = db_connect($this->dbname);
$result = $dbconn->query($query);
After that, I think I made some foundations for creating my objects.
I started with creating an Artist object and I'm stuck with some turbo-basics I think...
First of all, If my object has lots of properties, should I make getter and setter for each of them? Should I, or is it advisable?
Ok, I tried to write some simple method called getArtist($artistID) and I think I successfully created an object 🆒
When I call my class from some other php file I can easily handle all the properties of that object.
Next, I tried to write a method called getAllArtists(). I managed to loop through my query and created an array of objects, something like this;
$allArtists = array();
//some query...
while ($row = $result->fetch_object()){
$newArtist = new Artist();
$newArtist->setArtistName($row->artistName);
$newArtist->setArtistDesc($row->artistDesc);
$allArtists[] = $newArtist;
}
If I var_dump or print_f $allArtists I can see that it is an array full of Artist objects.
I wonder if I'm doing all this right :rolleyes:
Ok, i return $allArtists in getAllArtists() method and I don't know what next... When I instantiate my Artist class in some other file I don't know how exactly to manipulate with data from getAllArtists()...
I'm also not sure where should I do 'formatting' for my data from classes?
For example, I have idea to somehow format my objects in a specific way (with html, css, etc) and after that to use them in such way... Is something like that possible? =)
Is all this actually that famous M-V-C pattern everybody's talking about??
Any ideas/advices/thoughts about my dilemmas would be really appreciated!
Thanks a lot!