On Tue, Jul 14, 2009 at 6:21 AM, Anton Heuschen<anto...@gmail.com> wrote:
> This is just a general question,
>
> I am not 100% on when to use global $var , and $this->var  and how/what
> about the GLOBAL vars ....
>
> Lets say I have one file I  call config.php ....here I connect to the db, to
> ldap etc .... the connection "var" I can then use in a file on its own ...
> obviously after I include "config.php"  .... lets say in config.php my DB
> connect was $dbconnect ....
>
> In my index.php page I then use $dbconnect again .... but do I simply use
> $dbconnect again ... or must I say global $dbconnect and then use it in the
> rest of the DB calls? or use GLOBALS .. Within a class I can use $this->var
> correct ... but its not something to be used in a basic "procedural" if I
> can call it that page...
>
>
> Lets say with my config.php and its connection to the db ...where I have
> $dbconnect ...... in a class I can also use it, do I access  this var
> straight as $dbconnect or use $this->dbconnect = $dbconnect (and define it
> as global $dbconnect first before doing this)
>
> I am getting my results and seems to working most of the time, but not sure
> if I am using calls to global or $this->var ..when its not required and
> calling the var direct would of sufficed.
>
> I have never really used GLOBAL vars, so not sure how this ties in or if it
> might be even more helpful ...
>
> Some suggestions or pointers or examples would be appreciated just to clear
> up some confusion.
>
>
> Regards
>
> Oh and if one class uses methods in another class .... do I instansiate a
> new object of the other class .... I have seen use of OtherClass::Method
> ....  is this better method of $obj = new OtherClass()  use
>

You're really opening a big can of worms here, but it'll be a good
adventure.  Just keep at it and try reading some real books on the
subject.

If you include a file, all of those variables are magically in the
current scope.  So when you include config.php inside your index.php,
you can use $dbconnect directly.

Use $this-> when you are inside a class using a dynamic call on a
method or property of that class.

class Foo {
  protected $bar;
  public function __construct() {
    $this->bar = 'wee';
  }
  public function setBar($value) {
    $this->bar = $value;
  }
}

Inside the class you would use this-> to reference bar or call any of
that classes methods/props.  Outside you would use it like this:
$foo = new Foo;
$foo->setBar('blah');

If you haven't used globals yet, please do not feel compelled to do so
now.  There are all sorts of ways of dealing with passing around your
application state.  Globals can be used by a skilled programmer of
course, but I'd shy away from them.

I'd also recommend reading some of these pages:
http://www.php.net/manual/en/language.variables.scope.php
http://www.php.net/manual/en/language.oop5.php


Hope this helps!

-- 
http://www.ericbutera.us/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to