Jump to content

My First Class


TomTees

Recommended Posts

Okay, so I want to take a stab at my first PHP class.

 

Here is what I want it to do, but I'm not sure where to begin?!

 

I have an HTML form in my index.php file, and when the user clicks "Register", I'd like to have registration.php which will contain the class Registration take what was entered in the form and print it out on the user's screen.

 

This is an academic example, but it will get my feet wet!

 

So, some questions...

 

1.) If registration.php is basically my Registration class, then where do I "instantiate" the class to create a "registration object"?

 

 

2.) How do I pass what was entered into the form to my registration class?

 

Can you pass arguments to a class while you are instantiating it?

 

Or do I instantiate the class, and then pass values to the object?

 

Or do I let the object somehow get the values?

 

 

3.) I assume if I make it this far, then I can display the values by just having echo statements in the class/object, maybe as a method?

 

Thanks,

 

 

TomTees

 

 

Link to comment
Share on other sites

1.) If registration.php is basically my Registration class, then where do I "instantiate" the class to create a "registration object"?

 

Usually I put the class in one file, eg classes/Registration.php.  Then I would include that in registration.php (with require_once('class/Registration.php')), and instantiate the class in registration.php

 

2.) How do I pass what was entered into the form to my registration class?

 

Can you pass arguments to a class while you are instantiating it?

 

Or do I instantiate the class, and then pass values to the object?

 

Or do I let the object somehow get the values?

 

Yes to the first two options, and I wouldn't recommend the third option.  A lot of classes implement both those first two options.  But the third option is mixing the interface (where the data comes from, and where it's displayed) with the processing logic (in the Registration class), which I generally don't recommend.  It can make sense in some situations though.

 

3.) I assume if I make it this far, then I can display the values by just having echo statements in the class/object, maybe as a method?

 

Again, don't mix your interface with the logic!  Have your class return values only, and the output can be done by the code that instantiated and uses the class.

 

If you need to format your values in a specific way, you can have the class do formatting, but make sure it returns the string instead of outputting it directly.  This makes it flexible, as you can then use the result in any way you want.  If the class prints it directly then it reduces your possibilities, as you can't modify the output without modifying the class.  Same with the input - have the class take a data structure as input, and let the outside code handle converting $_GET or $_POST or $_REQUEST or any other data source into what the class expects.

Link to comment
Share on other sites

Just to be sure you're clear on this, when btherl mentions a class' interface, he's not referring to the built-in language construct named interface.  Every class has an interface - the public fields and methods of that class.  This interface is how the rest of your code interacts with your classes and objects.  It creates and enforces particular lines of communication between the classes or objects and your client code.

 

Why is this important?  One of the key concepts of OOP is encapsulation.  Objects should essentially be treated as black boxes.  Data is entered or a request for data is made, but how that data or request is processed remains a mystery.  All that matters, at least in the eyes of the client code, is that the right thing happened.  One would expect a public method named getValue() to retrieve a value.  How that value is actually obtained doesn't matter to your client code.

 

This all leads into abstraction and polymorphism, other key concepts you'll need to learn if you want to do OOP right.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.