Scope of Variables and use of global and this->var
Scope of Variables and use of global and this->var
am 14.07.2009 12:21:04 von Anton Heuschen
--001485f26fd4c5feda046ea7cb00
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
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
--001485f26fd4c5feda046ea7cb00--
Re: Scope of Variables and use of global and this->var
am 14.07.2009 13:53:03 von Eric Butera
On Tue, Jul 14, 2009 at 6:21 AM, Anton Heuschen wrote:
> This is just a general question,
>
> I am not 100% on when to use global $var , and $this->var  and how/w=
hat
> about the GLOBAL vars ....
>
> Lets say I have one file I Â call config.php ....here I connect to th=
e 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 t=
he
> rest of the DB calls? or use GLOBALS .. Within a class I can use $this->v=
ar
> 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 va=
r
> straight as $dbconnect or use $this->dbconnect =3D $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 su=
re
> 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 cle=
ar
> 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 =3D 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 =3D 'wee';
}
public function setBar($value) {
$this->bar =3D $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 =3D 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!
--=20
http://www.ericbutera.us/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Scope of Variables and use of global and this->var
am 14.07.2009 15:01:10 von Darren Karstens
> 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
> .... =A0is this better method of $obj =3D new OtherClass() =A0use
The :: is used to access static methods of a class. Static methods can
be used without creating an instance of the class because they dont
use any of the classes member variables.
For example say you have a class with a function for calculating the
area of a rectangle:
class SomeMathFunctions {
public function calculateRectangle($width, $height) {
return $width*$height;
}
}
To use this function you would need to first create an instance of the
class then call the method using the normal -> :
$funcs =3D new SomeMathFunctions();
$area =3D $funcs->calculateRectange(10,15);
But if you create the function as static by using " public static
function calculateRectangle($width, $height) { "
then you can access the method by using just 1 call:
$area =3D SomeMathFunctions::calculateRectange(10,15);
So for creating utility functions its better to use static methods
since you dont get the overhead of creating a new instance of the
class.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Scope of Variables and use of global and this->var
am 14.07.2009 15:44:02 von Martin Scotta
do you need to use global?
IMO you should use just 1 global variable, thats is what I call "entry poin=
t"
My scripts looks like...
require_once 'loader.php';
Loader::registerAutoload();
$foo =3D new Foo();
$foo->doStuff();
This way you can develop faster and do maintenance better avoiding
problems with third-party.
Here you have some rules for remember how to access
you want a $var from outside and you are outside an object or
function? =3D> use the $var
you want a $var from outside you are inside an object or function? =3D>
global $var o $GLOBALS['vars'] (better to pass it as arg)
you want a $var from an object and you are inside the same object? =3D>
use $this->var (better $this->getVar() )
you want a $var from an object and you are inside other object? =3D> use
$object->getVar() or Class::getVar()
It is a good practice to declare the object members as "protected" and
provide s/getters for each member (when your design allow it). Also
you can overload by using the __get, __set and __call
It is really easy to make an "automagic" object
Class AutoMagic
{
protected $_vars =3D array();
=09
public/*mixed*/
function __get(/*string*/$name)
{
return isset( $this->{ $name } ) ? $this->_vars[ strtolower($name) ] : nu=
ll;
}
=09
public/*mixed*/
function __set(/*string*/$name,/*mixed*/$value)
{
return $this->_vars[ strtolower($name) ] =3D $value;
}
=09
public/*boolean*/
function __isset(/*string*/$name)
{
return array_key_exists( strtolower($name), $this->_vars );
}
=09
public/*void*/
function __unset(/*string*/$name)
{
if( isset( $this->{ $name } ))
unset( $this->_vars[ strtolower($name) ] );
}
=09
public/*mixed*/
function __call(/*string*/$method,array $args)
{
$type =3D strtolower( substr( $method, 0, 3 ) );
$property =3D substr( $method, 3 );
=09
switch( $type )
{
case 'get':
return $this->{ $property };
=09
case 'set':
if( !array_key_exists(0, $args) )
trigger_error( 'Bad call in ' . get_class($this) . '::' . $method
..'. Method needs an argument' );
=09
return $this->{ $property } =3D $args[0];
=09
case 'has':
return isset( $this->{ $property } );
=09
case 'del':
unset( $this->{ $property } );
return;
}
trigger_error( 'Bad call in ' . get_class($this) . '::' . $method );
}
}
On Tue, Jul 14, 2009 at 10:01 AM, Darren
Karstens wrote:
>> 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
>> .... =A0is this better method of $obj =3D new OtherClass() =A0use
>
> The :: is used to access static methods of a class. Static methods can
> be used without creating an instance of the class because they dont
> use any of the classes member variables.
> For example say you have a class with a function for calculating the
> area of a rectangle:
> class SomeMathFunctions {
> =A0 =A0public function calculateRectangle($width, $height) {
> =A0 =A0 =A0 =A0return $width*$height;
> =A0 =A0}
> }
>
> To use this function you would need to first create an instance of the
> class then call the method using the normal -> :
> $funcs =3D new SomeMathFunctions();
> $area =3D $funcs->calculateRectange(10,15);
>
> But if you create the function as static by using " public static
> function calculateRectangle($width, $height) { "
> then you can access the method by using just 1 call:
> $area =3D SomeMathFunctions::calculateRectange(10,15);
>
> So for creating utility functions its better to use static methods
> since you dont get the overhead of creating a new instance of the
> class.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--=20
Martin Scotta
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Scope of Variables and use of global and this->var
am 15.07.2009 23:28:04 von TedD
At 12:21 PM +0200 7/14/09, Anton Heuschen wrote:
>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 ..
Anton:
My way -- every time I open a database, I do so by including the
configuration.php file that holds the logon/password et other data to
connect with the database. When I'm done with what I want from the
database, I close it. I do not store anything in GLOBALS.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Scope of Variables and use of global and this->var
am 16.07.2009 15:53:25 von Govinda
On Jul 15, 2009, at 3:28 PM, tedd wrote:
> My way -- every time I open a database, I do so by including the
> configuration.php file that holds the logon/password et other data
> to connect with the database. When I'm done with what I want from
> the database, I close it.
If one does not close it, then what are the consequences? And do any
consequences persist, and how? Or is it just a consideration for a
limited time? What limits the risk?
In case there is a good article about this, I'd love a link..
Thanks!
-G
> Cheers,
>
> tedd
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Scope of Variables and use of global and this->var
am 16.07.2009 15:54:47 von Eddie Drapkin
On Thu, Jul 16, 2009 at 9:53 AM, Govinda wrot=
e:
>
> On Jul 15, 2009, at 3:28 PM, tedd wrote:
>
>> My way -- every time I open a database, I do so by including the
>> configuration.php file that holds the logon/password et other data to
>> connect with the database. When I'm done with what I want from the datab=
ase,
>> I close it.
>
> If one does not close it, then what are the consequences? Â And do an=
y
> consequences persist, and how? Â Or is it just a consideration for a =
limited
> time? Â What limits the risk?
> In case there is a good article about this, I'd love a link..
>
> Thanks!
> -G
>
>> Cheers,
>>
>> tedd
>
If you're not using persistent connections, they'll all get closed
when the script completes.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php