Jump to content
xisto Community
Sign in to follow this  
vujsa

Php Classes I Just Dont Understand

Recommended Posts

I've learned every programing language I know by example. BASIC, JavaScript, PHP, Perl, HTML.

 

HTML I actually learned by printing a copy of the source code of the Indiana University index page and printing the page as well. Then I went line by line figuring out each command.

 

The only language I every actually had former instruction in is C++. You'll notice it's not in the list of programming languages I know.

 

So maybe I'm not a master programmer in any of these languages but I can accomplish most of my requirements. The downside is that many times I write much more code than I need to. I just don't know the better way.

 

I'm sure that using Classes in my scripts would make my life far easier and the scripts simpler if I knew how they worked.

The PHP Manual is less than helpful on this topic at least for me. I've tried other PHP websites but their information is just a duplicate of the PHP Manual.

 

So, if someone could please explain the concept and provide a useful example it would be very helpful.

 

Oh, by the way, feel free to over simplfy the expaination. :P

 

Thanks,

vujsa

Share this post


Link to post
Share on other sites

Look at classes like a template. In this template you've given it variables and functions to be used within this class.

e.g. you could create an invoice template, on this invoice you may have features like name, address, phone number, purchase order, invoice number etc.

How a class does this is it stores the variables and functions for all this, here's an example:

<?phpclass Invoice {var $name;var $address;var $date;var $phone;function Entry($name, $address, $phone){$this -> name = $name;$this -> address = $address;$this -> phone = $phone;}function Add_Date($day, $month, $year){$this -> date = "$day/$month/$year";}}$new_invoice = new Invoice;$new_invoice -> Entry('John Doe', '123 Someplace St', '555 5555');$new_invoice -> Add_Date('21', '02', '2005');print_r($new_invoice);?>


Now this is pretty basic, we can do more advance things, but for now I'll try to explain how we could efficiently use this type of class even if it's plain.

What we have done here is manually added our data to our object Invoice, we could have created a form to add this information, we could have created functions to automatically create the date etc, store it within a database, use the created variables to build our invoice page to be displayed etc.

To really understand classes you really need an idea of what you're trying to achieve, say for example you have a light bulb, the light bulb is the object (class), what can a light bulb do? Well switch it on, the bulb will shine brightly, switch it off, and it won't. We could create an object light bulb and can also create variables to store it's status and a function called switch to change it's status. Do you think you could write such a class?


There's a lot more that could be explained, I actually understand classes better in C++ than in PHP, but they are similar to some extent, but I would have to learn PHP classes stronger than what I already know now.

In the C/C++ Programming Section of this forum I created a calendar console program which uses Calendar as the class, you might want to have a look at that and see if you can make sense of it?


Cheers,


MC

Share this post


Link to post
Share on other sites

So the code provided returned this:

invoice Object ( [name] => John Doe [address] => 123 Someplace St [date] => 21/02/2005 [phone] => 555 5555 )

 

At least your example will output a result.

So print_r() shows me that the variable $new_invoice is actually an object named invoice.

And the object named invoice contains information about John Doe.

 

Am I reading this correctly?

 

If so, could you show an example of how to use the data contained in the object.

 

Also, if I wanted to use more than the one entry, I'd need to save the John Doe information first or use a variable key [$x] and an incremental command with $new_invoice

 

Basically, save $new_invoice in a database or flat file or change $new_invoice to $new_invoice[$x] and create an array.

 

Sorry, sometimes I need to re-explain things to myself to work it all out in my head and then to others to get feedback.

 

Thanks

vujsa

Share this post


Link to post
Share on other sites

Speaking of Php and classes. I read in paper some guy got suspended for talking about PHP. The teacher thought it was drug :P

<{POST_SNAPBACK}>


Hahaha.. that's neat phoenix47 :P vujsa - I see you got yourself a nice explanation of classes from mc. If you are still unclear about any particular part of classes (say how inheritence, polymorphism etc work) - I'd be able to point you out to a couple of good tutorials or better yet, explain some myself.. Do make a post in if you need any further help..

Regards :P

Share this post


Link to post
Share on other sites

You're reading it correctly.Forget the object for now, that's our class we are using, the reason I outputted what was inside was so you could see [variablenames] as that is quite important.To access any of these we use our [variablenames] we used to do this echo $new_invoice -> name; should result in showing John Doe, so that's how we can display what's contained in it.We can alter information as well, say we needed to update this information with a new phone number $new_invoice -> phone = '555 1234';now we print it out print_r($new_invoice); and notice that now we've altered the phone number.You would store this information, in some means, cookie, session, file, database but currently it's stored within these variables (memory). You could have a number of variables including$new_invoice[$x] = new Invoice;So that each $new_invoice[?] could contain information you've set. You could even create like a data entry way for doing things inside a while loop and while you're not finished continue allowing data to be inputted while the array keeps incrementing, after you're done you'll leave the while loop, store your information in whatever method you choose to store this data.Once you discover everything you would need for your class you can work on a 'well designed system', basically PHP should be written with classes, it simplifies the process and is reusable code, you could create many classes and just include the classes you need at the time.I may write a PHP tutorial on a class for email, validating, sending, creating, storing, etc. May even extend it to a mail system program, but for now, I only want to do a third of the work :PAt least this will give me a chance to show advance techniques, methods and just to show off and try to impress the girls...Cheers,MC

Share this post


Link to post
Share on other sites

MC

 

I tried to complete the assignment you gave me. How'd I do?

<?/*Assignment from mastercomputers -->To really understand classes you really need an idea of what you're trying to achieve,say for example you have a light bulb, the light bulb is the object (class), what can a light bulb do?Well switch it on, the bulb will shine brightly, switch it off, and it won't.We could create an object light bulb and can also create variables to store it's status and a functioncalled switch to change it's status. Do you think you could write such a class?*/class Lightbulb {    var $name;    var $status;    function Switched($name, $status) {        $this -> name = $name;        $this -> status = $status;    } // End function Switched()} // End class Lightbulb$new_lightbulb = new Lightbulb;$new_lightbulb -> Switched("Red", "On");print_r($new_lightbulb);?>
Returned:

lightbulb Object ( [name] => Red [status] => On )

 

I assume that when I finally understand the concept it'll hit me like a ton of bricks.

:P

 

Thanks,

vujsa

Share this post


Link to post
Share on other sites

Well, you've got the simple idea, but what if you wanted to use this class but have additional items without changing the class itself? Since there are more things a lightbulb can do, but your class did not have it.

Well I'll explain how we can extend our existing class and add more functionality to it.

e.g.

<?phpclass LightBulb  // simple lightbulb class{    var $status = 'Off'; // default status to Off    function SwitchPosition($status)  // our switch for the lightbulb    {        $this -> status = $status;  // we determine whether it's on or off    }}class Dimmer extends LightBulb  // add more onto our already existing LightBulb class{    var $brightness = '0%';  // default brightness to Off	function check_status($brightness)  // check to see if light is on by brightness	{  if($brightness == '0%') 	 $this -> status = 'Off';  // turn it off  else 	 $this -> status = 'On';  // turn it on	}    function SetBrightness($brightness)  // allows us to specify our brightness    {        $this -> brightness = $brightness;  $this -> check_status($this -> brightness);  // check to see whether it's on or off    }}$first_lightbulb = new LightBulb;print_r($first_lightbulb);  // see how it looks before alteringecho "<br /><br />";$second_lightbulb = new Dimmer;print_r($second_lightbulb);  // see how it looks before alteringecho "<br /><br />";$first_lightbulb -> SwitchPosition('On');  // switched on our first lightbulbprint_r($first_lightbulb);echo "<br /><br />";$second_lightbulb -> SetBrightness('50%');  // set brightness for second lightbulbprint_r($second_lightbulb); // by setting the brightness, we also set the inherited statusecho "<br /><br />";// fooling our class$second_lightbulb -> check_status($second_lightbulb -> SetBrightness('0%'));$second_lightbulb -> SwitchPosition('On');  // inherited objects can cause undesirable resultsprint_r($second_lightbulb);  echo "<br /><br />";?>

With the above code, we create a new class that also inherits everything our old class had. This way I could add a dimmer onto your class, without altering your code.

How do we really use classes in the real world?

We write classes that do all those repetitive things, as well as to shorten our code, maybe we want a class to take care of our web pages for us, e.g. titles, keywords, descriptions, meta tags, etc. We could write a class that does this, save it as ourHTML.class, include it in any of our PHP files and start with $variable = new ourHTML; and then start by filling in all our variables, we'd have functions that will display our page, layout, etc, and we have variables that store the information we want to send to it, like our Title for our site, etc.

If you want an example of a class that reduced pages of code (not PHP but in C++) is the MFC (Microsoft Foundation Class), at the time it took around 5 pages of code to make a simple window, along came MFC and turned 5 pages to 1 page. Imagine what those programmers were like when they used that class, it just made their life easier because MS took the hardwork out of it.

Soon you'll see where classes could come in handy, look for things you're repeating over and over, and simplify your process.

Remember the above applies to most if not all OOP languages.


MC

Share this post


Link to post
Share on other sites

MC,

Thanks for the reply.

 

Here's a small typo you may want to edit:

$first_lightbulb -> SwitchPosition('On);  // switched on our first lightbulb  # # # *** MISSING END QUOTE.print_r($first_lightbulb);echo "<br /><br />";

I have read your posts over ang over and I am beginning to understand how classes behave but their use is still unclear.

 

That's my vain way of saying that what you are saying makes sense but not to me.

I tend to identify a problem and then find the quickest most direct route to its solution before identifying another problem. So if I really concentrate, I can follow along with the class code but don't really understand what's going on.

 

I understand that the class collects variable information and uses functions to group the data into a predefined format but then what.

 

It's hard to explain what I don't understand because I don't know what I'm confussed about.

 

Thanks, :)

vujsa

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.