Re: [PHP] Scope of Variables and use of global and this-var

2009-07-16 Thread 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: [PHP] Scope of Variables and use of global and this-var

2009-07-16 Thread Eddie Drapkin
On Thu, Jul 16, 2009 at 9:53 AM, Govindagovinda.webdnat...@gmail.com wrote:

 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


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



Re: [PHP] Scope of Variables and use of global and this-var

2009-07-15 Thread 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




[PHP] Scope of Variables and use of global and this-var

2009-07-14 Thread Anton Heuschen
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


Re: [PHP] Scope of Variables and use of global and this-var

2009-07-14 Thread Eric Butera
On Tue, Jul 14, 2009 at 6:21 AM, Anton Heuschenanto...@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



Re: [PHP] Scope of Variables and use of global and this-var

2009-07-14 Thread Martin Scotta
do you need to use global?
IMO you should use just 1 global variable, thats is what I call entry point

My scripts looks like...

require_once 'loader.php';
Loader::registerAutoload();
$foo = 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? = use the $var
you want a $var from outside you are inside an object or function? =
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? =
use $this-var (better $this-getVar() )
you want a $var from an object and you are inside other object? = 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 = array();

public/*mixed*/
function __get(/*string*/$name)
{
return isset( $this-{ $name } ) ? $this-_vars[ 
strtolower($name) ] : null;
}

public/*mixed*/
function __set(/*string*/$name,/*mixed*/$value)
{
return $this-_vars[ strtolower($name) ] = $value;
}

public/*boolean*/
function __isset(/*string*/$name)
{
return array_key_exists( strtolower($name), $this-_vars );
}

public/*void*/
function __unset(/*string*/$name)
{
if( isset( $this-{ $name } ))
unset( $this-_vars[ strtolower($name) ] );
}

public/*mixed*/
function __call(/*string*/$method,array $args)
{
$type = strtolower( substr( $method, 0, 3 ) );
$property = substr( $method, 3 );

switch( $type )
{
case 'get':
return $this-{ $property };

case 'set':
if( !array_key_exists(0, $args) )
trigger_error( 'Bad call in ' . 
get_class($this) . '::' . $method
.'. Method needs an argument' );

return $this-{ $property } = $args[0];

case 'has':
return isset( $this-{ $property } );

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
Karstensdarrenkarst...@googlemail.com 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
   is this better method of $obj = new OtherClass()  use

 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 = new SomeMathFunctions();
 $area = $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 = 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





-- 
Martin Scotta

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