Jump to content

ready to learn function and class


Dorky

Recommended Posts

ok. i have a lot of php under my belt. i have yet to see a function that says to me "this is the way to go" but for sake of having a clue i feel i should learn this. if anyone is interested in explaining functions and classes(structure, advantages) in a way that can be put into practical use rather then text book theory, i would greatly appreciate it.

 

p.s. i have looked at php manual and w3schools.com and neither of them explained the how and why to the extent i could understand it. no i didn't post this question here because i'm lazy.

Link to comment
Share on other sites

well functions are incredibly useful. Even in procedural style coding, whenever you have code that is repeated, it makes your code a lot more readable, and it greatly reduces repeat code. A good example is a sanitation function. You may need to sanitize user input in a bunch of different places in your website, but instead of manually doing it every time, you can just create a function that takes an unclean string, and returns a clean one. This is incredibly useful on pages where you may need to sanitize a lot of input.

 

classes are also incredibly useful. There are a few things you have to understand, and apperciate before they seem useful though. The main thing is encapsulation, which is basically the practice of using classes to protect your data, and put your data in a sort of "container". for sake of ease, i'm not going to go over abstraction or polymorphism and I'll briefly mention inheritance.

 

Now here is an example (more theory than practical, but still)

class myClass {
private $var1, $var2, $var3;

public display(){
echo $this->var1, $this->var2, $this->var3;
}
}//end class

 

alright, so the first thing to note is setting the variables as private. This makes it so that only the class itself can access those variables. so doing something like

$object = new myClass;
echo $object->var1;

will not work, and will throw a php error

 

now why is this useful? This allows me to make sure that data that should be of a certain format, or should only have certain values does, because I take care of all that in my class. So if you were to work for a company, with several different programmers, or open source your project, programmers wouldn't accidentally give that variable bad information to screw up the entire script. A great example is the Facebook API (what developers use to make facebook applications) certain information should only be used by the class (for example, the user id of the current user) if you could change that, then you could make it appear as if a user was someone they werent!

 

Another example is a class I built at my job. It takes a query, and some other information, and builds am HTML table of the values so that another javascript class can filter it and other stuff. Now certain things in that class just shouldn't ever be altered by a programmer that is using the class. For example, the column names (which are retrieved from the query itself) Or the data returned. Setting these data members as private ensures that no one can mess with it except for the class.

 

Classes also help a lot with repeated code, since you can define methods inside classes. Also, because of encapsulation, we have a bunch of data that is within the scope of all the methods. In some cases, we would have functions that need (for example) 10 parameters that aren't define in the scope of the function, and we may need to set 5 other different variables. With encapsulation, we may only need 2-4 different parameters, and the rest come from the class. in a procedural script, we would need to pass the 10 parameters to use, and 5 more by reference to change. thats 15 different parameters!

 

Now don't get me wrong. You need to mess around with OOP before you can get a sense of when to use OOP and when OOP would just be pointless. Shorter scripts usually don't need OOP, but  when you have large applications that have a lot of similar data that is processed the same (and thus can be put into their own "containers" to handle the processing) OOP can become a hugely useful tool. Remember, programming is about using the tools your given in the best way. Sure a small screwdriver might be able to do the job of a large one, but using a large one results in more efficient, better applications. But sometimes using that large screwdriver is overkill. its all about the situation at hand

Link to comment
Share on other sites

any chance i could get you to break this down so i can understand what is happening at each of these steps?

class myClass {
private $var1, $var2, $var3;

public display(){
echo $this->var1, $this->var2, $this->var3;
}
}//end class

 

$object = new myClass;
echo $object->var1;

 

Link to comment
Share on other sites

corbin is right, you should definitely look into some tutorials, but about the code

 

class myClass {
private $var1, $var2, $var3;

public function display(){
echo $this->var1, $this->var2, $this->var3;
}
}//end class

 

this defines a class. you can define variables within the class, and set hem as private, public, or protected. private means that only the class can access them, and public means that anyone can access them. Ill explain this further with the next code snippet

 

the function display is called a method, because its a function within the class. there are a couple of things to notice. for a class to access its member variables, we use the $this keyword, with the array (->) character. if you are familiar with C++, java, etc. it is similar to the '.' operator. calling a method is similar. we use the same $this-> syntax, but with the method name. IE $this->display()

 

now the second snippet

 

$object = new myClass;
echo $object->var1;

the first line does what is called instantiating the class. Basically it means to create an "object" of the class. That object is now a container in your code with the classes data members and methods. Look at the second line. notice two things. WHen we access PUBLIC data members and methods, instead of the $this keyword (which is only usable inside classes) we use the variable name, which is now basically a reference to the object of class myClass.

 

The second thing to notice is that the second line won't work because the data member var1 in the class myCLass is defined as private, so only the class can use it. If we were to change the keyword private to public, than it would work in that case.

 

but honestly, as corbin said, the subject of OOP is far too huge to cover in one simple post. I didn't even go over constructors, protected, inheritance or anything like that. I really think you should read up on some tutorials and try it yourself.

Link to comment
Share on other sites

i have. they dont make sense. im sure a small script of each with a breakdown of what is happening in them step by step(one example each) would suffice. but so far what i see are folks using them to bypass other parts of php they dont understand. thats why in the manual section(as noted i have already tried) is full of post correcting post after post correcting post. im sure someone with real world exp with them and the understanding of the "why" not just remembering, could give the breakdown i seek. if you are not interested in responding to this thread other then to complain that i posted it or anything other then the advice i am seeking, dont waste my time to check your post or your time to write a reply. its simply counterproductive.

 

 

Functions and classes are not something that can be properly and fully explained in a brief forum post.  (Definitely not classes.... maybe functions.)

 

 

I suggest reading tutorials and articles about the two things.

Link to comment
Share on other sites

if you can't understand tutorials on functions or classes, perhaps you should read tutorials on more basic aspects of PVP. classes can be somewhat hard to understand, but functions are just a couple of lines of code that you can basically run with certain information and have it spit out more information. classes are like containers that have a certain set of data, with a certain set of functions that can alter that data.

 

what kind of breakdown do you seek. it doesn't take much real world experience to see how classes work. are you looking to be convinced that you should use classes? do you want a sample function with an explanation of every line? Also, what about the tutorials do you not understand. I have read the w3schools tutorials, as well as the manual, and it seems very clear to me.

Link to comment
Share on other sites

well if i was interested in whatever PVP is then i would consider wasting my time listening to you.

 

if you can't understand tutorials on functions or classes, perhaps you should read tutorials on more basic aspects of PVP. classes can be somewhat hard to understand, but functions are just a couple of lines of code that you can basically run with certain information and have it spit out more information. classes are like containers that have a certain set of data, with a certain set of functions that can alter that data.

 

what kind of breakdown do you seek. it doesn't take much real world experience to see how classes work. are you looking to be convinced that you should use classes? do you want a sample function with an explanation of every line? Also, what about the tutorials do you not understand. I have read the w3schools tutorials, as well as the manual, and it seems very clear to me.

Link to comment
Share on other sites

if you are not interested in responding to this thread other then to complain that i posted

 

I agree. I hate when people reply to a question with "go look it up in google" or "go read a manual". It's pretty rude.

It's not rude. I think it's pretty rude that some expect people to write them a book specifically for them when there are already thousands of other tutorials and examples out there. (Not specifically talking about this case, at least not totally.).

Link to comment
Share on other sites

and while this is one use of classes im sure this is not the limits of classes and it isnt really a step by step breakdown of code, being of practical exp this should be easy for you. im sorry if my questions make you uncomfortable but it is and was your choice to reply to this thread. and its really just for as i said "the sake of having a clue" that im interested in them. my target and existing clients do not require anything i would need this for.

 

corbin is right, you should definitely look into some tutorials, but about the code

 

class myClass {
private $var1, $var2, $var3;

public function display(){
echo $this->var1, $this->var2, $this->var3;
}
}//end class

 

this defines a class. you can define variables within the class, and set hem as private, public, or protected. private means that only the class can access them, and public means that anyone can access them. Ill explain this further with the next code snippet

 

the function display is called a method, because its a function within the class. there are a couple of things to notice. for a class to access its member variables, we use the $this keyword, with the array (->) character. if you are familiar with C++, java, etc. it is similar to the '.' operator. calling a method is similar. we use the same $this-> syntax, but with the method name. IE $this->display()

 

now the second snippet

 

$object = new myClass;
echo $object->var1;

the first line does what is called instantiating the class. Basically it means to create an "object" of the class. That object is now a container in your code with the classes data members and methods. Look at the second line. notice two things. WHen we access PUBLIC data members and methods, instead of the $this keyword (which is only usable inside classes) we use the variable name, which is now basically a reference to the object of class myClass.

 

The second thing to notice is that the second line won't work because the data member var1 in the class myCLass is defined as private, so only the class can use it. If we were to change the keyword private to public, than it would work in that case.

 

but honestly, as corbin said, the subject of OOP is far too huge to cover in one simple post. I didn't even go over constructors, protected, inheritance or anything like that. I really think you should read up on some tutorials and try it yourself.

Link to comment
Share on other sites

lol, wow, REALLY. i dont expect anyone to do anything for me, read the original post "if anyone is interested" makes it more then clear (as if it wasnt already) this is voluntary to begin with. i dont recall anyone being required to respond to my post. you voluntarily decided to show you #$$ is the truth of the matter. go argue with you girlfriend or grandmother. im not interested.

 

if you are not interested in responding to this thread other then to complain that i posted

 

I agree. I hate when people reply to a question with "go look it up in google" or "go read a manual". It's pretty rude.

It's not rude. I think it's pretty rude that some expect people to write them a book specifically for them when there are already thousands of other tutorials and examples out there. (Not specifically talking about this case, at least not totally.).

Link to comment
Share on other sites

lol, wow, REALLY. i dont expect anyone to do anything for me, read the original post "if anyone is interested" makes it more then clear (as if it wasnt already) this is voluntary to begin with. i dont recall anyone being required to respond to my post. you voluntarily decided to show you #$$ is the truth of the matter. go argue with you girlfriend or grandmother. im not interested.

 

if you are not interested in responding to this thread other then to complain that i posted

 

I agree. I hate when people reply to a question with "go look it up in google" or "go read a manual". It's pretty rude.

It's not rude. I think it's pretty rude that some expect people to write them a book specifically for them when there are already thousands of other tutorials and examples out there. (Not specifically talking about this case, at least not totally.).

 

I'm not exactly sure what your issue is. I simply pointed you to a source that if read would likely get you the understanding your seeking. Its up to you to read it or not.

Link to comment
Share on other sites

LOOK. I POSTED THIS TO FIND SOMEONE INTERESTED IN HELPING ME UNDERSTAND SOMETHING A CLEARLY STATED DID NOT MAKE SENSE TO ME IN THE TUTORIALS I HAVE READ. WHY ARE ALL OF YOU SO PREDISPOSED TO NEGATIVITY. IF IT DOESNT INTEREST YOU THEN LEAVE IT ALONE. FIND ANOTHER THREAD TO HELP WITH. BUT FOR GOD SAKE GET OFF MY CASE. IF YOU CANT SAY ANYTHING NICE-DONT SAY ANYTHING AT ALL

Link to comment
Share on other sites

MY ISSUE IS YOU INSINUATED I AM RUDE WHEN IT WAS ANOTHER USER THAT USED THAT WORD TO BEGIN WITH NOT ME. AS WELL AS SAYING I EXPECTED ANYTHING OF YOU OR ANYONE. ITS VVVVVVVOOOOOOOOLLLLLUUUUUUNNNNNNTTTTTAAAAARRRRRYYYYY DU!

 

lol, wow, REALLY. i dont expect anyone to do anything for me, read the original post "if anyone is interested" makes it more then clear (as if it wasnt already) this is voluntary to begin with. i dont recall anyone being required to respond to my post. you voluntarily decided to show you #$$ is the truth of the matter. go argue with you girlfriend or grandmother. im not interested.

 

if you are not interested in responding to this thread other then to complain that i posted

 

I agree. I hate when people reply to a question with "go look it up in google" or "go read a manual". It's pretty rude.

It's not rude. I think it's pretty rude that some expect people to write them a book specifically for them when there are already thousands of other tutorials and examples out there. (Not specifically talking about this case, at least not totally.).

 

I'm not exactly sure what your issue is. I simply pointed you to a source that if read would likely get you the understanding your seeking. Its up to you to read it or not.

Link to comment
Share on other sites

OOPS - LOOKS LIKE SOMEONE LEFT THEIR CAPS ON..... or they are like 12 years old and trying to show that they have their panties in a wad by not appreciating the help they are getting (and yes you are getting help, they are posting links to good tutorials/articles.)

 

Why don't you look at the tutorials, and try the example code the tutorials provide, and when you have a specific question about why this is better/why this works/etc, then post that.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.