Jump to content

Basic OOP


unemployment

Recommended Posts

I'm trying to learn OOP.  I mean I just started today :) and I don't understand why this doesn't echo anything.  Please help.

 

class user_info
{
public $first_name 	= 'Jason';

public function info_data(){
	global $first_name;

	echo $first_name;
}
}

$data = new user_info();
$data->info_data();

Link to comment
Share on other sites

Your first step is to forget that you ever saw the php global keyword, in procedural code or OOP. Don't use global.

 

That's not how you reference a class property/variable inside a class method. You use the $this->class_property syntax. Here's the first example from the php.net OOP documentation -

 

<?php
class SimpleClass
{
    // property declaration
    public $var = 'a default value';

    // method declaration
    public function displayVar() {
        echo $this->var;
    }
}
?> 

 

Link to comment
Share on other sites

Your first step is to forget that you ever saw the php global keyword, in procedural code or OOP. Don't use global.

 

That's not how you reference a class property/variable inside a class method. You use the $this->class_property syntax. Here's the first example from the php.net OOP documentation -

 

<?php
class SimpleClass
{
    // property declaration
    public $var = 'a default value';

    // method declaration
    public function displayVar() {
        echo $this->var;
    }
}
?> 

 

Thanks.  Ok that makes sense, but why shouldn't I use global?  Global is used a lot in the php documentation from what I can tell.

Link to comment
Share on other sites

Globals are bad

 

If we're both working on a PHP project, and you take a variable defined in the global scope, and modify that value in the local scope of a function without any reference to it, I get very mad.

 

I expect that variable to have a certain value, and some function changes it. The function call doesn't have any reference to it. I now have to comment out each line until I find out WHERE that value has been changed, or GREP through my entire project searching for 'global [^;]*?\$varname'

 

Heck, if someone was really mean, they'd do something like

<?php

$var = 'somevalue';

function pissyouoff() {

$hidden = 'var';
$GLOBALS[$hidden] = strrev($GLOBALS[$hidden]);

}

pissyouoff();

echo $var;

?>

 

Now imagine pissyouoff() was included from another file, and there was about 20-30 more user-defined functions called on that page :P

Link to comment
Share on other sites

Globals are bad

 

If we're both working on a PHP project, and you take a variable defined in the global scope, and modify that value in the local scope of a function without any reference to it, I get very mad.

 

I expect that variable to have a certain value, and some function changes it. The function call doesn't have any reference to it. I now have to comment out each line until I find out WHERE that value has been changed, or GREP through my entire project searching for 'global [^;]*?\$varname'

 

Heck, if someone was really mean, they'd do something like

<?php

$var = 'somevalue';

function pissyouoff() {

$hidden = 'var';
$GLOBALS[$hidden] = strrev($GLOBALS[$hidden]);

}

pissyouoff();

echo $var;

?>

 

Now imagine pissyouoff() was included from another file, and there was about 20-30 more user-defined functions called on that page :P

 

that was a very interesting way of describing it.... ::)

Link to comment
Share on other sites

Would you prefer if I used words like namespace pollution, non-locality or implicit coupling?

 

I was trying to put it into simple terms with an example

 

Yes, globals can be used for quick one-offs, and singleton classes are pretty much globals confined to a class, but you should only use them when you completely understand why you shouldn't use them.

Link to comment
Share on other sites

Would you prefer if I used words like namespace pollution, non-locality or implicit coupling?

 

I was trying to put it into simple terms with an example

 

Yes, globals can be used for quick one-offs, and singleton classes are pretty much globals confined to a class, but you should only use them when you completely understand why you shouldn't use them.

 

that was a very defensive response to a playful post....geeze man lighten up.. and namespace polution would not make much sense to use here now would it.. :thumb-up:

Link to comment
Share on other sites

Thanks.  Ok that makes sense, but why shouldn't I use global?  Global is used a lot in the php documentation from what I can tell.

 

No, it's not.

 

Anyway, as to why 'global' is bad....

 

Functions should be seen as black boxes.  Their innards (function definition) should be hidden from view.  All that one needs to know about a function in order to properly use it is its signature - its name, explicitly required parameters, and return type (which is kind of glossed over in PHP). 

 

'Global' adds implicitly required parameters to the mix, thereby muddying up the function's signature.  Further, it breaks the whole idea of how a function should work, as it creates a link in the function to the environment it will be used in.  Rather than being abstract black boxes which can be used in a variety of situations, they're now bound to a certain kind of context.  The problem only magnifies as the use of 'global' continues.  Code is dependent on other code with no real indication of why.  A change in one part of the system would likely necessitate a change in another.  This is known as coupling, and is considered very bad.  And, if the required 'global' is missing, or not of the proper type, it can be very difficult to find and fix, requiring one to actually dive deep into the code rather than simply look at the function signatures to get a feel for what's going on.

 

Besides, 'global' is completely unnecessary anyway.  There's already a mechanism to pass variables into a function - the argument list.  Use it, love it, and if you feel you're passing too many parameters into a function, chances are your design could be improved.

Link to comment
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.