Jump to content

Learning Php Oop - First System


rysalo4ka

Recommended Posts

Hi,

 

I have some experience with PHP but i decided to move on to OOP (and design patterns).

The idea is to read things that i find relative and try and implementing it in a system. I wanted something that will need OOP and can be something fun and i thought of an idea.

The problem is that because of years of a mish-mash code where html + JS+PHP all together + control C and control V habits, i find it difficult to begin.

I want to do it the "right way" and not middle way - to use design patterns where they are needed and do a correct PHP.

 

Will be glad for tips about the beginning.

 

My idea (the simple)

I want to build a site where people can register and choose who are they friends (from other registered people)

then a person can open an event (for example: birthday, wedding, baby shower) and under the event he can list presents he really wants. He can add links to specific items and pictures.

His friends will see it and they can check out gifts that they want to buy

the user that opened the event wont see who checked what but other guests will see it, so they wont buy the same gift

also if a gift is expensive, people can partially check it with the amount of money they can give and when the amount is reached, an email will be send to all the pariticipants in the gift

 

The reason for the idea is all those baby showers with the same gifts that people dont want or need to exchange, moving to new home parties where people really need small stuff but receive things they dont need and generally for people who want to buy an enjoyable gift but dont know what the person needs.

 

The idea (complicated)

Login with facebook

different gift levels - really need, my wish, i will enjoy

an ability to see what the person liked or got so even if he didnt open an event, you know what to give him

different visibility levels - the person doesnt know who will buy what, the person knows what was checked out but not who did it, full visibility

chat between guests so they can discuss the list

and so on...

 

 

For now i have questions, just to make sure i got this correct:

 

1. Design patterns are basically ideas of a correct class structure that is used for a specific problem. Each design pattern solves some problems. For example singleton pattern is basically an idea on how to create a DB class. 

 

2. The problem that i am currently unable to find a solution for - is what is the correct way to implement GUI. I saw couple of ways:

              1. The old html page with php embeded into it - this i want to avoid

              2. Some people just gather all the information they need from the server and send this as jason and implement the GUI only on a client view- i am not sure why i dont like this idea, but i dont.

             3. I saw an example of a class, called "form" that basically used a php class that echo all the html needed for a form - it is a very long way to implement something rather simple, and i am still not sure if this is the correct way

             4. Something else?

 

3. What do you think of my idea, how to begin implement it? The main concern is to make something that i wont be able to make bigger in the future. 

4. Mysqli VS PDO - i am using Mysql server and i am familiar with Mysqli. Is it really better to learn PDO or i can stick with the Mysqli library?

 

Link to comment
Share on other sites

The correct way to implement a GUI is to use the MVC structure, splitting your code into controllers (handles input from the user, figures out what data to retrieve and what views to display), models (usually an object that includes methods that get/set/edit/delete specific data), and views (anything that needs to be shown to the user -- primarily HTML, with snippets of PHP where appropriate to include data). I would suggest checking out http://www.killervideostore.com/video-courses/php-projects.php, specifically, the "PHP Login with OOP and MVC" series, or do research on MVC online. 

 

As long as you aren't using "regular" MySQL, either MySQLi or PDO should be fine.

Link to comment
Share on other sites

 

4. Mysqli VS PDO - i am using Mysql server and i am familiar with Mysqli. Is it really better to learn PDO or i can stick with the Mysqli library?

 

Either is fine but it seems the major PHP web frameworks are leaning towards PDO. 

 

Other than that, stick to the standards and you should be fine for the most part.

Stef

Link to comment
Share on other sites

First - thanks for the replies! I got laughed at in other forums i posted in, because i dared to say that doing it in non OOP way is not the best way in my opinion...

Of course i dont use MYSQL. When i started learning PHP, i began already with MySQLI.

 

Other questions: why is there a leaning towards PDO? the only difference i have found is the fact that PDO works with different DBs. but if i write my class in a correct way and later i will move to another DB, then i supposed to re-write only the DB class, right?

 

I know about MVC and i used it in .NET, I dont know why, but it is very hard for me to understand the implementation in PHP.

 

I supposed to have a model  == class for each "major" table in DB, right? (user, event, gift and others)

The controller is also a class? For each model?

In the view - should  i have some PHP? Is it supposed to be just an HTML+JS? 

Link to comment
Share on other sites

I am also trying to read now about design patterns. Should i have a pattern for each class? Or maybe its better begin implementing the classes and then see how i can improve it with some design pattern?

 

and i still dont understand why is it better to write an interface and only then implement it in a class.

Link to comment
Share on other sites

This is how my first ideas go about the classes but i know that its not for example an MVC approach and i dont know how to change it to MVC:

class DB: connection and manipaltion to DB. field: con and all the operations with it are functions.

class user: user can register, can unregister, add friends, add events, delete friends, delete events, add gifts, delete gifts, add friends to events, delete friends from events, paricipate in a gift, buy a gift.
fields: all the user information, array for events.

class event: we can create an event, add a gift to event, change event permissions, delete event, edit an event
fields: event information an array for gifts

class gift: create a gift, edit gift, delete gift, being chosen, send email to the gift buyer
gift information (name, links, picture, cost)

and also how to represent views? are they also classes?

Link to comment
Share on other sites

Everyone handles MVC differently. Possibly the more advanced you are, the more complicated you get... I tend to stick to a relatively simple approach if I'm not using a framework like Laravel. You can see the approach in my videos, but at least in the way I handle it, I tend to structure my application like this:

 

Controllers:
these are procedural PHP files, they will usually load a central initialization PHP file of some sort, which may create any standard variables/constants the entire app uses, as well as creating any common objects (perhaps creating a database connection, loading an authorization class if there needs to be login functionality, etc.) In addition, a controller will create objects for any specific pieces just that controller needs. With that in place, the controller will check for any user input (GET data if the URL is created in a specific way), or POST data if there are forms involved, and determine how to proceed or what to display to a user based on this input (or lack of input).

 

Basically, the controllers handle the flow of the application, loading models as necessary, processing user input, and finally displaying the correct view(s) to the user.

 

Models:

these are my PHP classes. I usually have a class for every piece of related functionality, and if a class gets too large, it's usually time to refactor the code, possibly into different classes. For example, in your list above, I would say your user class is too large, and you should make sure that functionality related to events and gifts are in separate classes. It might be a good idea to create a separate "Friends" class, that handles creation/editing/deletion of relationships between users.

 

I tend to have a "Template" object, which helps manage and control my views. It can perform common functionality, like storing data that needs to be passed to a view, storing temporary data (for example, alert or error messages), and displaying the views themselves. For example,  I might have a line in my controller:

 

$Template->save_data('name', 'value');

$Template->display('users/edit', 'public');

 

The first line would save a specific piece of data to the $Template object. The data is stored in an array, and can be retrieved later by a view by looking in the array for the array key "name". The second line would instruct the template object to display a view file, edit.php, within the "users" folder, using the "public" template. Templates allow me to save common areas of a website, such as headers and footers, so perhaps the "public" template would include a public header/footer, for regular visitors, whereas a "private" header and footer might contain information that only should be displayed to logged in users.

 

Views:

As I said above, in my approach, views are displayed to a user by calling $Template->display(), which will include() the correct file(s). These view files end in .php, but consist primarily of HTML (I'm assuming CSS and Javascript are stored in separate files, but are linked in the view), with minor bits of PHP to retrieve data from the Template class where appropriate. For example, say you wanted to show a "hello, username" line at the top of the page. Your controller would pass the username to the $Template object with a call to $Template->save_data(), where the Template would temporarily store it in an array. When the view is called, the file would consist primarily of HTML, but it would ask the Template object for that username using a little bit of PHP:

 

...html here...

Hello, <?php $this->get_data('username');

...html here...

 

I'm using "this", because if your view is included() using the Template object, the application is within the scope of the Template object, and "this" will refer to the "Template" object. The Template object will then check its data array for a piece of data named "username", and either echo it out, or echo nothing out if the variable doesn't exist.

 

 

I hope that helps get you started?

Link to comment
Share on other sites

First, I want to thank you for your detailed explanation, it is indeed, very helpful! Couple of points, raised by your reply:

 

 

1. Why advancing in OOP causing to complicate the MVC pattern? I supposed that we have some level of complication which is the best and more of it will cause the program to be too complicated, not?

 

2. I want to confess that my own projects were written on notepad++. I still didn’t figure out why it is better to use a framework and what to choose, since there are so many of them.

3. I liked your template object and I will think of its implementation. I also felt that my user class it too big but I didn’t know how to make it smaller. Thanks for the advice. Do you have one template class to handle replies for all the views? Each controller is building its own template object and pass it to the view?

 

 I will try later to sketch a beginning for my program and will be happy for your comments regarding the classes. 5

 

So as I understand it:

 

Model = php classes. You have a php file per class, right?

 

Controllers are mainly functions that use the php classes, create objects and maintain the logic between what we show on the screen and what the user gave us as an input and the DB

 

 

Regarding the views:

 

I like your approach. It means all the logic is handled by classes (and then by the controller) and the view uses the output in an array or something else. Is there a reason why you prefer this way and not the way I saw in one of the books, where they build php classes that build a View (for example form class that echo a form, based on different parameters)

 

 

And of course css and js are in different files.

Link to comment
Share on other sites

1) Maybe I misspoke. I think what I meant to say was not that the MVC pattern itself gets more complicated, but that your overall structure of your application may get more complicated as you advanced, depending on the features and functionality you need. The one big thing that I don't include in my structure, which other more complicated options do, is some sort of pretty URLs and routing. For example, rather than "user.php?id=1&anotherid=2" the system might be set up to use "user/1/2". Doing something like that requires using mod_rewrite and passing all requests through a main index.php file, then having the system figure out what controller needs to be loaded.

 

2) Frameworks just allow you to build on a solid foundation. You don't have to worry about things like setting up a template object, the models/views/controllers all have specific locations and specific ways of being loaded, there are often common functionality built in (database classes, email classes), and the common codebase allows people to write and extend the framework, and share the code so you can use it as well. It can speed up the process of building an application. This is slightly old by now, but worth a look: http://code.tutsplus.com/tutorials/why-laravel-is-taking-the-php-community-by-storm--pre-52639

 

3) One central Template object that is created by an init.php file, that all my controllers load at the start.

 

 

"Model = php classes. You have a php file per class, right?"

Yes.

 

"Controllers are mainly functions that use the php classes, create objects and maintain the logic between what we show on the screen and what the user gave us as an input and the DB"

Yes, though mine are usually procedural -- no functions needed. Anything that requires a function might go in a shared functions.php file, where the entire application could have access to those functions, or it's a function that should really be part of a model. Depends on the situation.

 

" Is there a reason why you prefer this way and not the way I saw in one of the books, where they build php classes that build a View (for example form class that echo a form, based on different parameters)"
I'm not sure what books you are looking at, and I'm not exactly sure how/why they took that approach. But from what you have told me, I wouldn't consider the book's approach MVC. Unless perhaps you had some sort of Form Helper object that helped generate parts of your views?

Link to comment
Share on other sites

So - here are my thoughts about system implementation. What do you think?

 

First- the Views

1. Index.php - the main page view. We have explanation about the system and Login\Registration buttons. The buttons open a pop up window which allow them to register or to login to the system

2. UserPage.php - the main page for a register user. This page has:

                                      User picture and name and a link to edit user personal data.

                                      A list of all created events and an option to delete or edit them.   An option to create event.

                                    A list of all the events that user was invited to.

                                     A list of all gifts that the user decided to buy for his friends with a delete\edit button

                                  A tab with his friends.

3. CreateEvent.php - here the user creates the event and adds gifts that he want. Each gift will be added in a pop-up button. Then you can choose which friends to add to the event.

4. EditEvent.php - i am not sure if it the same page as create, just with all the data inside or for simplicity a new page

5. Event I was invited to - the event page as the user that was invited to - sees it. He can see all the info of the event, choose to participate in a gift or to buy a whole gift.

6. Friends.php - a list with all my friends with an ability to add a friend or delete him.

 

Second - the DB

The tables that i thought i should have. I am not sure if i want 3NF or 2NF

 

User table: all the user personal Data. PK: UserID

Event table: all the event personal data. PK: eventID

Gift table: all the gift personal data. PK: giftID

 

GiftInEvent table: Gift Id and Event ID

UserInEvent: UserId, EventId, UserRole (is it the owner or invited)

Friends: UserID1 , UserID2

 

What do you think? Should i build it differently? 

Link to comment
Share on other sites

Now are the classes in here is the hardest part since here i am a total NOOB :) and i am a little bit confused on how to represent the connections between friends, event and gift and event and users and which functions are needed

 

<?php
/*Models*/
class DB{} //will think of it later :)
 
 
class user{
private $id;
private $name;
private $password;
private $email;
private $birthdate;
private $picture;
private $UserinEvents[]; // here we have an array of the class UserInEvent with all the relevant events to this user.
private friends; //class friends
//Functions
function __construct(){}
getters & setters
function edit_user_details()
function _user()
}
 
 
class friends{
private $id; //of the user that called this function
function __construct(id){}
function add_friend();
function delete_friend();
function show_friend(idOfTheUser);
function show_all_friends()
}
 
 
class UserInEvent{
private $userID;
private $eventID;
private $roleID;
}
 
class event{
private $id;
private $name;
private $description;
private $date;
private $timeStart;
private $timeEnd;
private $place;
private $eventType //myEvent or EventForSomeone
private $gifts[]; //array of gift class
private $userOwner[]; //class user. We can have as admin a person who created the event and others he made an admin
 
 
function __construct(){}
getters & setters
function showAllGifts(){}
function editEvent()
function deleteGift()
}
 
 
 
class Gift{
private $name;
private $link;
private $description;
private $picture;
private $cost;
private $wantLevel; // From 1-nice to have to 3-Heart desire
private $chosen; //the gift is chosen by someone to buy it - then chosen=1. Not chosen=0. Partly chosen = 2
 
function __construct(){}
getters & setters
function editGift()
}
?>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...