vujsa
Feb 21 2005, 01:35 AM
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. Thanks, vujsa
Reply
mastercomputers
Feb 21 2005, 02:49 AM
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: CODE <?php
class 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
Reply
vujsa
Feb 21 2005, 04:09 AM
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
Reply
phoenix47
Feb 21 2005, 04:39 AM
Speaking of Php and classes. I read in paper some guy got suspended for talking about PHP. The teacher thought it was drug
Reply
miCRoSCoPiC^eaRthLinG
Feb 21 2005, 05:57 AM
QUOTE(phoenix47 @ Feb 21 2005, 11:39 AM) Speaking of Php and classes. I read in paper some guy got suspended for talking about PHP. The teacher thought it was drug  Hahaha.. that's neat phoenix47  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
Reply
mastercomputers
Feb 21 2005, 08:25 AM
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  At 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
Reply
vujsa
Feb 21 2005, 09:21 AM
MC I tried to complete the assignment you gave me. How'd I do? CODE <? /* 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 function called 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. Thanks, vujsa
Reply
mastercomputers
Feb 21 2005, 02:01 PM
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. CODE <?php
class 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 altering echo "<br /><br />";
$second_lightbulb = new Dimmer; print_r($second_lightbulb); // see how it looks before altering echo "<br /><br />";
$first_lightbulb -> SwitchPosition('On'); // switched on our first lightbulb print_r($first_lightbulb); echo "<br /><br />";
$second_lightbulb -> SetBrightness('50%'); // set brightness for second lightbulb print_r($second_lightbulb); // by setting the brightness, we also set the inherited status echo "<br /><br />";
// fooling our class $second_lightbulb -> check_status($second_lightbulb -> SetBrightness('0%')); $second_lightbulb -> SwitchPosition('On'); // inherited objects can cause undesirable results print_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
Reply
vujsa
Feb 23 2005, 07:45 AM
MC, Thanks for the reply. Here's a small typo you may want to edit:CODE $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
Reply
Recent Queries:--
php - how to create an invoice - 581.38 hr back. (1)
Similar Topics
Keywords : php, classes, understand
- How To Understand A Database Schema
A very nice and simple tutorial (4)
Simple Java Question
One Variable; Multiple Classes (3) I’m probably the only person that is able to do so much with Java3D and yet still can't
even get some of the Java basics down. Anyway is it (and if so how is it) possible to make a
variable editable and readable in multiple class files? It seems like this should be an easy
question to answer, but here is a little code to help you understand what I am talking about. CODE
public class test extends Applet{ public int Col = 0; if(Col==1){ } } class
TestB extends Behavior { if(collision == true){ Col = 0; }else{ Col = 1; } } Tha....
I Dont Understand Cpanel!
(12) Hi all, I am new to this entire Astahost thing, I have used Tripod's Site builder my entire
life!( Click Here To Visit Tripod Hosting ). First of all, when I go to my new website that I made
using Astahost(I think, see below) I see text that says something like "Welcome to your new website!
Please log in to cPanel by going to yourdomain.trap17.com/cpanel and change your email account,
blah...blah...blah" Visit my site at azerothheroes.trap17.com for full "View". Second, How do you
use cPanel at all? Where do I go to view/change my websites pictures/text and chang....
C++: Basic Classes
classes, objects, access labels, members, inline functions (5) This tutorial assumes that you have a basic knowledge of C++. You know how to use built-in types,
like ints, doubles, chars, etc. You should know some types that are part of the STL, like vector,
etc. Those types that come in the STL are just C++; you can create your own types just like those!
Non built-in types are referred to as classes . To create a class, you just use the keyword class
, the name of the class, and curly brackets. CODE //A class named MyClass class MyClass { };
In fact, that is all we need to create variables, pointers, or references to t....
Questions On Islam
Some things I don't understand about the religion (18) I understand now. /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0"
alt="biggrin.gif" />....
Creating Classes
(0) Im using Eclipse at touchofdeathproductions.com Im having trouble creating new classes other than
warrior. Could anyone teach me how? And
how to change the picture of your guy, like if i wanted to create a class called wizard, how
would i do that and change the picture of the warrior that you see when you are registrating.
/unsure.gif" style="vertical-align:middle" emoid=":unsure:" border="0" alt="unsure.gif" /> ....
Object & Classes Trouble
(2) The following is the code that I am testing with. QUOTE Public Class Form1 Private
sckCom As New MSWinsockLib.Winsock Private sckDat As New AxMSWinsockLib.AxWinsock Private
Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sckCom.Close() sckDat.Close() End Sub End Class The sckCom.close() method works
fine, but the sckDat.close() method causes the following exception :- Exception of type
'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown. I am not su....
Multithreaded Listview Control In Different Classes Problem
(3) I maybe crossing into the advanced questions on this one but I have been working on this problem for
a little over a week now and I am just plain stuck. I have a windows application in VB.NET. The
main form has a listview with approx 15 items and five subitems in each item. The list view acts as
a display grid for data. The application performs many operations at once so naturally I have made
it threaded to keep the main form responsive. The threads are running in their own classes. To
give you a general idea see below. CODE Public Class frmMain … Dim l....
Wordpress Rss
i need to understand (1) could someone help me understand the way wordpress creates rss feeds. i need to figure out what the
address will be to my feeds so that i can put them up on the website so that visitors who are not
very tech can easily access them. my experimental feed is here . is the address needed by a
feeder - thundebird for example - just that address + /feed.bla or something. i tried /atom.xml but
that didnt work and i sort of got stuck. thanks in advance....
Help: Export Classes From A Builder Dll To Vb6
(1) I'm writting a large class hierarchy and want to put it on a dll. What must I do to be able to
use the class definitions in the dll from a visual basic application?. I'm making the dll from
Borland C++ Builder 6 Enterprise. Thanks, Herenvardö....
Hosting Suspended And I Don't Understand Why...
(1) The Administrator claims that I have posted up spam within the past week that I have been here but I
cannot recall doing so. Is there anyway of finding out what posts triggered this?....
Xoops
I don't understand (1) I'm a noob at this sort of thing. I've set everything up, I just don't understand where
it all goes. Where can you view what you've set up? Thanks!....
Object Orientated Programming Help!
I just can't understand OOP!! (2) I'm coming here not because I expect anyone to post a step by step of what it is but because
hopefully someone here has links to a website that explains it really well or maybe knows of a
script that uses it in a clear example. Any links would be greatly appreciated. I just need to
understand it to see if using it could save me some time and effort. Thanks Kage This topic
deals with General Programming Concept. So I guess it should be in Programming > General. Topic
moved. Oopsadaisies. I should have mentioned that I want to learn the use of OOP in PHP, such ....
Flooble
I don't understand it (4) I'm not good with html, but i am trying to get a flooble as Xanga only supports flooble, can
someone please explain how do i change the colours and all, i get very confused sometimes.......
Looking for php, classes, understand
|
*SIMILAR VIDEOS*
Searching Video's for php, classes, understand
|
advertisement
|
|