Jump to content

Recommended Posts

Hey,

 

Since I haven't been practicing any other programming languages before (except pascal but there I used no classes and VB where the classes were automatically declared) now I've come across classes in PHP and I am not really aware of their purpose and what they're supposed to do.. I tried googling it but I couldn't find any good article about it, if someone could provide me one I'd be really greatful, thanks!

Link to comment
https://forums.phpfreaks.com/topic/242520-classes/
Share on other sites

Like thorpe said classes encapsulate behavior and state. Without classes you would be looking at global variables and functions that manipulate those variables. However you do not control access to those global variables so the variables your functions depend on can be modified by any 3rd party script you introduce since both you and the 3rd party script happen to have a $user variable. To avoid any name collisions you would use a class or now with 5.3 namespaces (although namespaces are not entirely safe).

Link to comment
https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245643
Share on other sites

Erm.. Okay but what's the point of grouping objects when you can simply not ? It's getting so complicated I don't get the point of classes O.o

 

Classes are blueprints.

 

say an architect makes a blueprint for a house and wants to create 50 of them houses.. he is not going to 50 separate blueprints for the same house.

 

say you're making a game, you make a player that can jump swim walk run and attack. 2 months later you make another game with a player that does the exact same thing. are you going to want to rewrite the code or simple use a class that already exists.

 

so your player class has methods call jump swim walk attack...etc.

 

all you would need to do is something like (this is not php code..)

 

New player = New PlayerClass();

 

now to get him to jump you would do..

 

player.jump();

 

or..

 

if(player touches water){

  player.swim();

}

 

and so on

 

not sure with PHP but something like Actionscript or Java if you want to round 23.34 down you would type Math.floor(23.34);

Math is a abstract class and floor is the method of the class... if there were no classes you would have to create the code to do this every time you needed it.

 

 

if you are starting out do not dive straight into classes and oop until you have a strong feel and understanding of the code... im still learning myself so most of this is probably wrong :)

 

Link to comment
https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245650
Share on other sites

In procedural you would write something like:

 

function login($username, $password) {
    global $user;
    if (login successfull) {
        $user = $userdata;
    }
}

 

In OO you would write:

 

class User {
    private $data;
    
    public function login($username, $password) {
        if (login successfull) {
            $this->data = $userdata;
        }
    }
}

 

The difference is that in procedural:

 

1. You do not control access to the $user variable which allows name collissions with 3rd party scripts and thus unexpected/uncontrolled behavior in your code.

2. Your function becomes highly dependent on the context it is used in (only works if a $user variable exists). This is also why they advice you not to use global variables inside your functions.

Link to comment
https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245653
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.