(Nov 24, 2012 02:29 PM)Eight Wrote: Without trying to sound like a complete douchebag, there are some things I'd like to point out :
> PHP by default is not OOP, you have to make it OOP.
Hmm.. The idea is there but the terminology is not quite right. PHP is a multi paradigm language, meaning you are free to write object oriented or procedural code but the language's structure itself is not fully object oriented and you can't make it so. What I mean to say is that it's not a "mode" that you can activate the the php.ini file or something.
> As you can see, the class name does not have to be the same as the file name, like how it is in java, etc.
It doesn't, but it's highly recommanded especially when making autoloaders for your classes. Look up the PSR standards. Technically nothing prevents you from putting two classes in the same file and giving it a strange name but then you would be coding like a pig.
> As you can see, we defined it using the $this-> method.
Did you try this ? It gave me a syntax error. This syntax is only valid inside your methods. To give your properties a default value, you can simply do public $variable = 3. If the value requires some relatively complex computations (return value of a function or a multi-line associative array or something), then you can do those inside the constructor, or write specific methods for them then call those from your constructor. For example, you can't do public $rand = rand() but you can do :
PHP Code
<?php class OOP { public $rand; public function __construct() { $this->rand = rand(); } }
> Now, as you can see, we are creating a new instance of the OOP class and running it through our handler.
I think you meant object, an object is the result of instantiating a class.
> And whala, you are done!
Voilà* :^p
In addition to what the other folks said, I would recommand you use require_once instead of require, this will ensure you don't include the same class definition twice and end up with "class already defined in.." errors. That being said, I think this is a very basic tutorial, in an introduction to OOP I would expect at least some basic, uhm, lexicology ? I mean, what a class is, what an object is, what encapsulation is, what methods and properties are, etc. Then some basic syntax, the different visibility keywords and their meaning, inheritance, and maybe abstract classes and interfaces. I attended a few "intro to C++" classes in the university and that's what the teacher taught them, though he began by stating the differences between C & C++.
Holy lord that was a lot of feedback!
Thanks, and as a result, I have added most of what you explained into the tutorial. I also added some definitions and notes throughout the tutorial to help explain it more to new users. :)
Also gave you some credits, since a lot of my additions were on feedback you gave me.
What is the purpose of classes. I know how to code them but don't know why someone would use it rather then just write functions?
I know what people told me that it's reusable and cuts down development time and does reduce having to rewrite code or repeat code.
Can someone here give us an example of a real world way of using a class.
Just write us some code or tell us when you have used classes and why. Why did you make the class rather then use functions.
I think I am not the only one that have these feelings. I do know now the uses of it. I seen tutorial videos of a person in PHP writing classes for different monsters. You can then inherit methods from one class to another or inherit properties.
I would like an example. I think real world examples are best.
(Nov 24, 2012 03:31 PM)colody32 Wrote: What is the purpose of classes. I know how to code them but don't know why someone would use it rather then just write functions?
I know what people told me that it's reusable and cuts down development time and does reduce having to rewrite code or repeat code.
Can someone here give us an example of a real world way of using a class.
Just write us some code or tell us when you have used classes and why. Why did you make the class rather then use functions.
I think I am not the only one that have these feelings. I do know now the uses of it. I seen tutorial videos of a person in PHP writing classes for different monsters. You can then inherit methods from one class to another or inherit properties.
I would like an example. I think real world examples are best.
We used a class to make the object. Then we can reference the object to use functions outside of our handler/file. It's a confusing concept to some people. All in all, it's mainly a neat template we can use to program. It's not required at all, but is recommended. It's also good practice if you program Java, as OOP PHP and Java are extremely alike (along with some other programming languages).
(Nov 24, 2012 03:35 PM)GaBaNo Wrote: We used a class to make the object. Then we can reference the object to use functions outside of our handler/file. It's a confusing concept to some people. All in all, it's mainly a neat template we can use to program. It's not required at all, but is recommended. It's also good practice if you program Java, as OOP PHP and Java are extremely alike (along with some other programming languages).
I never programmed in java. I don't think I be learning that. I will take your word for it that it's similar to php.
I own 2 websites and haven't used classes. I am thinking once I am finished with the projects. I am thinking to rewrite the code to use classes.
I wrote the website piss poor. I now understand my mistakes but will eventually have to go back and rewrite everything. However, before I do that I rather learn more about optimizing the websites code and obfuscation. I am not talking about PHP to obstructed but talking about javascript and html. I just want to make my source code not readable when you first look at it. You have to do some extra work to read the source code.
I was told the only solution to that is to do an based64 encoding. The data encoded in base64 will be able to be readable by the browser. I am not sure if that's true or not.
(Nov 24, 2012 03:44 PM)colody32 Wrote: I never programmed in java. I don't think I be learning that. I will take your word for it that it's similar to php.
I own 2 websites and haven't used classes. I am thinking once I am finished with the projects. I am thinking to rewrite the code to use classes.
I wrote the website piss poor. I now understand my mistakes but will eventually have to go back and rewrite everything. However, before I do that I rather learn more about optimizing the websites code and obfuscation. I am not talking about PHP to obstructed but talking about javascript and html. I just want to make my source code not readable when you first look at it. You have to do some extra work to read the source code.
I was told the only solution to that is to do an based64 encoding. The data encoded in base64 will be able to be readable by the browser. I am not sure if that's true or not.
You can use Javascript to base64 decode an encoded page and it will display it on any browser that supports Javascript, as it is a built in function.
(Nov 24, 2012 03:47 PM)GaBaNo Wrote: You can use Javascript to base64 decode an encoded page and it will display it on any browser that supports Javascript, as it is a built in function.
but the browser cannot natively understand encoded data in the base64 form?
(Nov 24, 2012 04:21 PM)colody32 Wrote: but the browser cannot natively understand encoded data in the base64 form?
No, it cannot. Only if the browser supports javascript, and if you have javascript enabled.
(Nov 24, 2012 04:37 PM)GaBaNo Wrote: No, it cannot. Only if the browser supports javascript, and if you have javascript enabled.
Ya, if javascript is enabled and working. If I had html code and javascript code encoded in base64. Would the browser accept the encoded data without me having to directly decode it?
I know the eval function where I can run a decode type function.
However, was told I don't need it. Was told the browser can understand base64 encoded data or character code.
I just want to hide the html and javascript of my website. I want to avoid having to decode it for the browser. I was told you can either encoded it in base64 or convert the characters to their numerical value.
Nov 24, 2012 05:02 PM
(This post was last modified: Nov 24, 2012 05:02 PM by CarrotMilk.)
There's no way you can hide front-end code. Don't even bother. Not worth it.
(Nov 24, 2012 05:02 PM)CarrotMilk Wrote: There's no way you can hide front-end code. Don't even bother. Not worth it.
This is completely true. I think view-source is pointless. Just another way for leechers to get some rep for something they didn't do <.<
(Nov 24, 2012 05:16 PM)GaBaNo Wrote: This is completely true. I think view-source is pointless. Just another way for leechers to get some rep for something they didn't do <.<
Viewing webpage sources is not pointless and it can be very helpful if you are a front-end developer.
Also I believe CM was saying that it is pointless to try and encrypt or obfuscate it because it can be decrypted any which way.
(Nov 24, 2012 05:34 PM)0xStack Wrote: Viewing webpage sources is not pointless and it can be very helpful if you are a front-end developer.
Also I believe CM was saying that it is pointless to try and encrypt or obfuscate it because it can be decrypted any which way.
I understand what he was saying. My point was that being able to see someone else's code is sort of stupid sometimes.
(Nov 24, 2012 05:37 PM)GaBaNo Wrote: I understand what he was saying. My point was that being able to see someone else's code is sort of stupid sometimes.
Well, how do you expect browsers to be able to render said code without, well...seeing it?
#OpenSourceYOLO
(Nov 24, 2012 05:40 PM)CarrotMilk Wrote: Well, how do you expect browsers to be able to render said code without, well...seeing it?
#OpenSourceYOLO
Well, I get that... but an option that allows kids to leech and see code?
(Nov 24, 2012 05:53 PM)GaBaNo Wrote: Well, I get that... but an option that allows kids to leech and see code?
Holy shit you sound like Apple. The world would be no where without sharing creations.
|