0

Hey guys,

I'm hoping ye can help guide me here as I've reached a point in PHP where I think I can say I'm no-longer a beginner and would like to advance my knowledge.

I believe my next step is understanding and using classes in PHP but for the life of me I can't understand the concept(why not just include other PHP files?). So could someone please explain why we whould use them(in simple terms), a link to a good easy to understand tutorial on classes and also if you could suggest an easy class to make to get me started.

Or if you think there is something other than classes I could be lookin at could you please let me know? Thanks.

5
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by xaeon
0

Hey guys,

I'm hoping ye can help guide me here as I've reached a point in PHP where I think I can say I'm no-longer a beginner and would like to advance my knowledge.

I believe my next step is understanding and using classes in PHP but for the life of me I can't understand the concept(why not just include other PHP files?). So could someone please explain why we whould use them(in simple terms), a link to a good easy to understand tutorial on classes and also if you could suggest an easy class to make to get me started.

Or if you think there is something other than classes I could be lookin at could you please let me know? Thanks.

Hi, I am also just beginning with classes and objects, I started with C++ hoping to get it down, but I think I was a little young then (10 or so) to get the concepts. Now, older and (hopefully) wiser, I think I can do a so-so explanation of classes in PHP.

A class is pretty much a way to create a reusable piece of code for your program, somewhat like a function but more complex a way as it can contain several functions within itself, and can interact with other classes based on this. They can have their own variables set, each independent from other instances of the class, and functions that are run in the class that depend only on the variables in that class, and the global variables you declare.

One goes about declaring a class as such:

<?php
class myClass() {
     $phrase = "This phrase is inside of the class"

     function sayPhrase() {
          echo $phrase;
     }
}

$classInstance = new myClass(); //Declare the class instance

$classInstance->sayPhrase();//Run the function sayPhrase()
//This would echo the phrase "This phrase is inside of the class"
//However, you can also set the variable to whatever you want:

$classInstance->$phrase = "The phrase is now user defined";
//Now the instance of the class (the "object"), has a $phrase variable that is set to "The phrase is now user defined"
//So running...
$classInstance->sayPhrase();
//now would echo "The phrase is now user defined" to the screen.

I hope this helps, again I am also beginning at classes, but maybe my simpler standpoint will be easier to understand. I'm pretty sure what I said was all correct to, if not, sorry ahead of time! :)

I know the concept is pretty hard, and here is an article that may help you: http://php.net/manual/en/language.oop5.php

0

well.. I would say that using OOP in php is rarely the right way to program, and it's not necessarily "Advanced"

What I can tell you about OOP in general is:

a class contains:
1. Functions: which do stuff to the object that belongs to a the class it lies in. For example a class might be a car and a function might be moving it, or stopping it

2. attributes: those are variables that characterize the object that belongs to the class, for example if the class was the car, an attribute would be the maximum speed or the length of the car.. etc

These are the main things about a class.

Know that the attributes and functions of a class are either public or private (and there is some kind "Protected" but you might not need this). so the public attribute or function, it can be accessed by other classes, but if it's private, it can be accessed only by the class it lies in.

Sorry I was not straight to the php OOP, but I never needed it.

0

To gain a better understanding as to 'why' use OO-PHP, perhaps you should look into some of it's more useful design patterns, it took me a while to understand MVC (the model view controller) design pattern, however now I find I am using it on a daily basis, separating logic from design helps add clarity to your code, and when working with a designer it will make the process of integrating templates much easier.

MVC / other design patterns are not the essence of OO coding, but should give you an idea of why many people prefer this method.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.