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



[PHP] Scope woe

2009-06-30 Thread Luke
Hello again guys,

I was wondering the best way to tackle the following problem:

I've got a class, containing a property which is another object. So from
outside I should be able to do
$firstobject-propertycontainingobject-methodinsidethatobject();

The $firstobject variable is in the global namespace (having been made with
$firstobject = new FirstObject;), and I'm having a problem that I'm sure
many people have when accessing it inside another class, so:

class otherObject
{
static function messwithotherthings ()
{
$firstobject-propertycontainingobject-methodinsidethatobject();
}
}

But $firstobject is blank, which makes sense because in there it is pointing
to the local variable within the method.

To solve this, I could add 'global $firstobject' inside every method, but
this is very redundant and boring. I've tried a couple of things like
adding:

private $firstobject = $GLOBALS['firstobject'];

But apparently that's bad syntax. I was just wondering the best way to get
around this?

Thanks a lot for your help,

-- 
Luke Slater
:O)


Re: [PHP] Scope of the variables around PHP class

2008-09-23 Thread clive
Define a class function and pass the array via this function or pass it 
via the classes constructor.


VamVan wrote:

Hello Guys,

I have a problem here. I hope you can help me resolving it.

Please see the code below

array.php has $array1 = ('hello'='heelo',)

require_once('array.php');

class Classa {

}

How can I access the array values in my class? I want to understand the
scope.

Thanks

  




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



[PHP] Scope of the variables around PHP class

2008-09-22 Thread VamVan
Hello Guys,

I have a problem here. I hope you can help me resolving it.

Please see the code below

array.php has $array1 = ('hello'='heelo',)

require_once('array.php');

class Classa {

}

How can I access the array values in my class? I want to understand the
scope.

Thanks


Re: [PHP] Scope of the variables around PHP class

2008-09-22 Thread Chris

VamVan wrote:

Hello Guys,

I have a problem here. I hope you can help me resolving it.

Please see the code below

array.php has $array1 = ('hello'='heelo',)

require_once('array.php');

class Classa {

}

How can I access the array values in my class? I want to understand the
scope.


You can't, unless you make $array a global variable which is not a 
proper solution.


You could do something like this:

class MyClass {

  private $my_array = array();

  function __construct()
  {
require_once ('array.php');
$this-my_array = $array1;
  }

}

Then you can use $this-my_array inside the class and it's automatically 
loaded when you create a new object.


--
Postgresql  php tutorials
http://www.designmagick.com/


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



[PHP] scope and return array( ); problems

2007-04-18 Thread Yvan Strahm

hi all,

I have this function:

function preg_array($pattern, $array, $r_array)
 {
   global $match;
   global $r_array;

   foreach ($array as $key = $value)
   {
 if (preg_match($pattern, $value,$match))
 {
   $GA=array_slice($match,1);
   break;
 }
   }
  return array ($GA, $match);
 }


[PHP] scope and return array( ); problems

2007-04-18 Thread Yvan Strahm

Hi all,

I have this code ( but you already know it ;-) :

function preg_array($pattern, $array, $r_array)
 {
   global $match;
   global $r_array;

   foreach ($array as $key = $value)
   {
 if (preg_match($pattern, $value,$match))
 {
   $r_array=array_slice($match,1);
   break;
 }
   }
  return array ($r_array, $match);
 }

$GA = array();
preg_array(/^GA\s+(\d+.\d+)\s+(\d+.\d+)/i, $PF, $GA)

but the array $GA is empty outside the function, i tried to declare $GA also
as global inside the function and it didn't change the output.
It works if the function is change to this :

function preg_array($pattern, $array, $GA)
 {
   global $match;
   global $GA;

   foreach ($array as $key = $value)
   {
 if (preg_match($pattern, $value,$match))
 {
   $GA=array_slice($match,1);
   break;
 }
   }
  return array ($GA, $match);
 }

What's wrong with the first version of the preg_array function? do I really
need to have the same array name in order to retrieve the third argument of
the function?

Thanks for your help,
yvan


Re: [PHP] scope and return array( ); problems

2007-04-18 Thread Richard Lynch
On Wed, April 18, 2007 5:33 pm, Yvan Strahm wrote:
 I have this function:

 function preg_array($pattern, $array, $r_array)
   {
 global $match;
 global $r_array;

Passing in $r_array *AND* declaring it global is just plain silly.

Do one or the other, but not both.

If the array is LARGE, you may also want to consider writing this with
array_map and a create_function so that you can have the iteration
happening in C instead of in PHP...

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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



Re: [PHP] Scope of include

2007-01-18 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2007-01-17 21:49:05 -0800:
 Hello php list:
 If I include a php script inside a php function definition and then
 call the function in another script. What is the scope of variables in
 the included script? Are they local to the function that calls include
 with the file name?

Yes.

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

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



Re: [PHP] Scope of include

2007-01-18 Thread tedd

At 9:49 PM -0800 1/17/07, jekillen wrote:

Hello php list:
If I include a php script inside a php function definition and then call the
function in another script. What is the scope of variables in the included
script? Are they local to the function that calls include with the file name?
Thanks in advance;
I'm not sure where to look for this answer;
JK



JK:

Whenever I have a question about an include, I just cut/paste (i.e., 
replace) the include statement with the include script. That way I 
know exactly what it does.


hth's

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: [PHP] Scope of include

2007-01-18 Thread Robert Cummings
On Thu, 2007-01-18 at 10:29 -0500, tedd wrote:
 At 9:49 PM -0800 1/17/07, jekillen wrote:
 Hello php list:
 If I include a php script inside a php function definition and then call the
 function in another script. What is the scope of variables in the included
 script? Are they local to the function that calls include with the file name?
 Thanks in advance;
 I'm not sure where to look for this answer;
 JK
 
 
 JK:
 
 Whenever I have a question about an include, I just cut/paste (i.e., 
 replace) the include statement with the include script. That way I 
 know exactly what it does.

Sounds like a maintenance nightmare :|

To answer his question though, variables not declared via the global
keyword and not accessed via the super global variables ($GLOBALS,
$_SESSION, etc) inherit the scope in which the include statement occurs.
Therefore, if in the included file all you have is:

$foo = 'foo';

Then it's scope is the function including the file.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

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



Re: [PHP] Scope of include

2007-01-18 Thread Stut

Robert Cummings wrote:

To answer his question though, variables not declared via the global
keyword and not accessed via the super global variables ($GLOBALS,
$_SESSION, etc) inherit the scope in which the include statement occurs.
Therefore, if in the included file all you have is:

$foo = 'foo';

Then it's scope is the function including the file.


It's important to have this clear in your head when dealing with 
included files, so I wrote an example that will hopefully demonstrate it 
for the OP...


http://dev.stut.net/php/scope/

-Stut

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



[PHP] Scope of include

2007-01-17 Thread jekillen

Hello php list:
If I include a php script inside a php function definition and then 
call the
function in another script. What is the scope of variables in the 
included
script? Are they local to the function that calls include with the file 
name?

Thanks in advance;
I'm not sure where to look for this answer;
JK

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



Re: [PHP] Scope of include

2007-01-17 Thread Andrew Kreps

On 1/17/07, jekillen [EMAIL PROTECTED] wrote:

Hello php list:
If I include a php script inside a php function definition and then
call the
function in another script. What is the scope of variables in the
included
script? Are they local to the function that calls include with the file
name?
Thanks in advance;
I'm not sure where to look for this answer;
JK


Does your included PHP script contain any functions?  Or is it more like:

file1.php contents:

function my_function () {
 if (i_feel_like_it) {
   include(inc.php);
 }
}

and inc.php contains variables at the global level such as:

inc.php:
$var1 = 2;
$var2 = 3;

Am I close?

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



Re: [PHP] Scope of include

2007-01-17 Thread Andrew Kreps

On 1/17/07, Andrew Kreps [EMAIL PROTECTED] wrote:

On 1/17/07, jekillen [EMAIL PROTECTED] wrote:
 Hello php list:
 If I include a php script inside a php function definition and then
 call the
 function in another script. What is the scope of variables in the
 included
 script? Are they local to the function that calls include with the file
 name?
 Thanks in advance;
 I'm not sure where to look for this answer;
 JK



FYI, I just ran a quick scope test based on my guess of your
situation.  If you include a PHP file containing global variables
inside a function, the variables will _only_ be available after you
include the file, and only for the duration of the function.  They
will not exist after the function ends, even if you specify global
$var1; in your other functions, or at the global level of your script.

This was tested using PHP 5.0.4.

Here is my sample code:

test.php:
?


print Top Level: Var1: $var1  Var2: $var2\nbr;

test1();
test2();

function test1 ()
{
print Before: Var1: $var1  Var2: $var2\nbr;
include(testinc.php);
print After: Var1: $var1  Var2: $var2\nbr;
}

function test2 ()
{
print Test2: Var1: $var1  Var2: $var2\nbr;
}

print Bottom Level: Var1: $var1  Var2: $var2\nbr;


testinc.php:
?
$var1 = 1;
$var2 = 2;
?

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



Re: [PHP] scope of classes in PHP

2006-02-13 Thread Jochem Maas

Stuart Bailey wrote:

Hi
I'm developing an app to administer LDAP (phpLdapAdmin is too complex for the 
customer).
I have created an LDAP class, in a file class_ldap.php. It include methods for 
connecting to the server, binding a DN (effectively login), and adding posix 
groups.


I have an index.php with include_once 'class_ldap.php', which allows me to 
login using the HTTP authentication mechanism.


The connect method stores the resulting connection resource in a global 
variable (included in a config.php file).
When I then go to add the posix group (from add_group.php, that also includes 
class_ldap.php), I find that the global connection variable is not valid.


My question is, do I need to connect and authenticate with the LDAP server 
every time I want to perform an action, or is there a way to establish a 


you need to establish a connection at the start of every request php handles,
typically you perform one  [major] 'action' per request so the short answer is:

'yes'.

the connection to the LDAP server is a 'resource' type variable - php 
'resources'
are only valid for the duration of the script.


connection for the user's session, that is cleared when the user logs out.

Many thanks,

Stuart.


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



[PHP] scope of classes in PHP

2006-02-11 Thread Stuart Bailey
Hi
I'm developing an app to administer LDAP (phpLdapAdmin is too complex for the 
customer).
I have created an LDAP class, in a file class_ldap.php. It include methods for 
connecting to the server, binding a DN (effectively login), and adding posix 
groups.

I have an index.php with include_once 'class_ldap.php', which allows me to 
login using the HTTP authentication mechanism.

The connect method stores the resulting connection resource in a global 
variable (included in a config.php file).
When I then go to add the posix group (from add_group.php, that also includes 
class_ldap.php), I find that the global connection variable is not valid.

My question is, do I need to connect and authenticate with the LDAP server 
every time I want to perform an action, or is there a way to establish a 
connection for the user's session, that is cleared when the user logs out.

Many thanks,

Stuart.
-- 
---
Stuart Bailey BSc (hons) CEng CITP MBCS
 LinuSoft (Proprietor)
   Linux Specialist
(01953) 601294
(07778) 383739
   http://www.linusoft.co.uk


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



Re: [PHP] Scope issue

2004-12-20 Thread Richard Lynch
GH wrote:
 Hi I am having an issue with I think it is the scope of variables:

 I have a file that I am including which has the following

 ?php
 // +--
 // | PHP Source
 // +--
 //

 echo got language.phpbr /;

 global $langauge;
 $language['project_name'] = P.L.I.M.S;
 $language['sub_project_name'] = DCR CC;
 ?

 In my main file, I am attempting to from with in a function call
 $language['project_name'] and i am failing... can you offer any
 advice?

Unless you are *inside* of a function definition, 'global' makes no sense
whatsoever in PHP.

Get rid of it unless you are inside function body.

But in your main script *DO* put:
global $language;
inside of your function body -- That's where you need it to be.

You may also want to consider passing $language in as an argument to your
function.

Or, if it *MUST* be a global variable, use $LANGUAGE so that it stands out
in the rest of your script.

-- 
Like Music?
http://l-i-e.com/artists.htm

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



[PHP] Scope issue

2004-12-17 Thread GH
Hi I am having an issue with I think it is the scope of variables:

I have a file that I am including which has the following

?php
// +--
// | PHP Source   
// +--
//

echo got language.phpbr /;

global $langauge;
$language['project_name'] = P.L.I.M.S;
$language['sub_project_name'] = DCR CC;
?

In my main file, I am attempting to from with in a function call
$language['project_name'] and i am failing... can you offer any
advice?

Thanks

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



RE: [PHP] Scope issue

2004-12-17 Thread Mike
 In my main file, I am attempting to from with in a function 
 call $language['project_name'] and i am failing... can you 
 offer any advice?
 

Are there any specific reasons that you need to set the variable to global
scope? 

It's typically recommended that unless you need to, to pass the variable
into the function and return the modified value if you plan on changing it. 

-M

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



[PHP] scope problem

2004-03-05 Thread Larry Brown
Apparently I'm having some kind of meltdown here.  Can anyone explain the
logic behind why the following variable has the original value and how I can
pull/push the value to access it at the end?


while loop
{
$variable = 100;
while loop
{
switch($othervar)
{
case 1:
$variable = $variable + 100;
break;
case 2:
$variable = $variable + 200;
break;
case 3:
$variable = $variable + 300;
break;
}
echo $variable.br;
}
echo brThe final value is .$variable.br;
}

This gives values something to the tune of...

200
400
700

100

I usually have variables set outside of a while loop that increment based on
the contents of the loop and I could swear that they hold the value on the
other side of the loop.  I don't usually use break; in my scripts unless I'm
using switch. However, I would think that if using break was throwing me,
that the value wouldn't print on each cycle of the loop.

TIA

Larry

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



Re: [PHP] scope problem

2004-03-05 Thread Richard Davey
Hello Larry,

Friday, March 5, 2004, 4:01:39 PM, you wrote:

LB This gives values something to the tune of...

LB 200
LB 400
LB 700

LB 100

Hard to say with so little code, but...

Your first where loop is probably running twice, i.e. resetting
variable back to 100 after the 2nd (internal) where loop has finished
modifying it. Move $variable = 100 above the first where loop and see
what happens.

-- 
Best regards,
 Richard Davey
 http://www.phpcommunity.org/wiki/296.html

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



RE: [PHP] scope problem

2004-03-05 Thread Larry Brown
Thanks for the help, but it turned out to be the nut on top of the keyboard!
Something I missed.  Good to know my concept of scope with php is still
healthy (at least).

Thanks

-Original Message-
From: Richard Davey [mailto:[EMAIL PROTECTED]
Sent: Friday, March 05, 2004 11:17 AM
To: PHP List
Subject: Re: [PHP] scope problem


Hello Larry,

Friday, March 5, 2004, 4:01:39 PM, you wrote:

LB This gives values something to the tune of...

LB 200
LB 400
LB 700

LB 100

Hard to say with so little code, but...

Your first where loop is probably running twice, i.e. resetting
variable back to 100 after the 2nd (internal) where loop has finished
modifying it. Move $variable = 100 above the first where loop and see
what happens.

--
Best regards,
 Richard Davey
 http://www.phpcommunity.org/wiki/296.html

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

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



[PHP] Scope: global $GLOBALS[]

2004-02-16 Thread Paul Furman
I checked the manual but I still don't get it.
http://us2.php.net/manual/en/language.variables.scope.php
I'm setting a variable $pics with a form by post method. It's on a 
screen that is included, then it needs to be applied to another 
screen that will take it's place when it's all re-processed.

template
  include header
  include SCREEN (picture or settings)
  include footer
Now, the template sets $pics to a default and then I can pull up the 
picture screen and setting $pics global makes it work but if I go to the 
settings screen to change $pic, it doesn't stick, even if I set it 
global there. I'm not sure if I need to set global once in the template 
or not there but in each screen I want to use it.



template:

global $pics;
if (!isset($pics)){$pics = 8;}
picture screen:

global $pics;
# works fine with the setting 8 from the template
settings screen:

global $pics;
print $pics; #for debugging, always follows template setting
if (!isset($pics)){$pics = 5;}
  if ($_POST){
if (isset ($_POST['thumbs'])){
  $pics = $_POST['thumbs'];
  print settings have been changed;
}
}
?

  form name=mail action=?php SCREEN_DIR . /settings.php ? 
method=post
  label for=thumbsnumber of thumbnails per page: /label
  input type=text name=thumbs id=thumbs size=2
 value=?php print $pics ?
  button type=submitOK/button
  /form

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


[PHP] scope of class atts in methods

2002-06-06 Thread Erik Price

Hi all,

a quick question about using PHP's objects, specifically in terms of the 
scope of class attributes:

Normally, in PHP, a variable in a function is local to that function and 
is NOT a reference to a similarly-named variable outside the function, 
right?  To the best of my knowledge, there are several ways to allow 
access to an outside variable from within a function:

1) Declare the variable as global with the global keyword
2) Access it from the $GLOBALS array
3) Pass the variable to the function as a parameter

and probably others, but it's really beside the point because I don't 
have a question about normal functions.  What I am really wondering is 
if there is any formal rules about the scope of Class Attributes in 
Class definitions and in methods -- they do not behave identical to 
PHP's functions.  For instance, I can access a class attribute from 
within a method of that class without explicitly declaring that 
attribute global inside the method, or without explicitly passing that 
attribute as a parameter to the method.  So they seem to behave as if 
they are always global.

But if I want to make a change to that class attribute from within a 
method, am I affecting a copy of the class attribute, or the class 
attribute itself?  The reason I ask is because I have a Class that is 
behaving oddly.  Here is the relevant part of the code:

Class Folder
{
// declare class attributes
var $contents = array();

// a method to add to $contents
function add_to_contents($item)
{
$this-contents[] = $item;
}

// a method to remove from $contents
function rm_item($index)
{
unset($this-contents[$index]);
}
}


The crux of my question is, does this have the effect that it appears to 
have?  I am hoping someone with thorough understanding of the internals 
of PHP can give me a definitive answer.  My tests are coming up with 
strange results, so I haven't figured it out on my own yet.

Thanks in advance,


Erik

PS: FYI, if you unset an array element, there is still an index for that 
element -- the array does not reindex itself.  A good solution to this, 
that a fellow lister named Nathan gave me, is to array_push() a dummy 
var onto the end of the array and then array_pop() it back off -- this 
reindexes the array.  But I am finding that somehow my arrays are 
remembering old elements that I could have sworn I unset, so I am 
asking the above question about the scope of class attributes in methods.










Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


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




Re: [PHP] scope of class atts in methods

2002-06-06 Thread Erik Price

If anyone read this post, you probably already forgot about it, but I 
just wanted to clarify that I found the source of the problem and it was 
a coding mistake on my part, not a problem with PHP's array 
implementation.



Erik



On Thursday, June 6, 2002, at 03:11  PM, Erik Price wrote:

 Hi all,

 a quick question about using PHP's objects, specifically in terms of 
 the scope of class attributes:

 Normally, in PHP, a variable in a function is local to that function 
 and is NOT a reference to a similarly-named variable outside the 
 function, right?  To the best of my knowledge, there are several ways 
 to allow access to an outside variable from within a function:

 1) Declare the variable as global with the global keyword
 2) Access it from the $GLOBALS array
 3) Pass the variable to the function as a parameter

 and probably others, but it's really beside the point because I don't 
 have a question about normal functions.  What I am really wondering is 
 if there is any formal rules about the scope of Class Attributes in 
 Class definitions and in methods -- they do not behave identical to 
 PHP's functions.  For instance, I can access a class attribute from 
 within a method of that class without explicitly declaring that 
 attribute global inside the method, or without explicitly passing that 
 attribute as a parameter to the method.  So they seem to behave as if 
 they are always global.

 But if I want to make a change to that class attribute from within a 
 method, am I affecting a copy of the class attribute, or the class 
 attribute itself?  The reason I ask is because I have a Class that is 
 behaving oddly.  Here is the relevant part of the code:

 Class Folder
 {
   // declare class attributes
   var $contents = array();

   // a method to add to $contents
   function add_to_contents($item)
   {
   $this-contents[] = $item;
   }

   // a method to remove from $contents
   function rm_item($index)
   {
   unset($this-contents[$index]);
   }
 }


 The crux of my question is, does this have the effect that it appears 
 to have?  I am hoping someone with thorough understanding of the 
 internals of PHP can give me a definitive answer.  My tests are coming 
 up with strange results, so I haven't figured it out on my own yet.

 Thanks in advance,


 Erik

 PS: FYI, if you unset an array element, there is still an index for 
 that element -- the array does not reindex itself.  A good solution to 
 this, that a fellow lister named Nathan gave me, is to array_push() a 
 dummy var onto the end of the array and then array_pop() it back off -- 
 this reindexes the array.  But I am finding that somehow my arrays are 
 remembering old elements that I could have sworn I unset, so I am 
 asking the above question about the scope of class attributes in 
 methods.










Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


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



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




[PHP] Scope problem in while loop

2002-03-11 Thread Randall Perry

According to the PHP 4 docs all variables are global unless within a
function. I've got the following test code which should output 2, but
outputs 1. The while loop creates it's own class object (which seems strange
since it isn't explicitly instantiated by my code; I would think it would
cause errors).

The reason I created an object for my variable (actually, I started testing
with regular strings) is that I've had similar scoping problems in perl,
where variables got out of scope within loops (or even if statements). In
perl, declaring an object outside these structures will protect it's scope.
Not so in PHP I see.

Can anyone explain this behavior? Do I have to create functions that return
values every time I need a loop that modifies a variable?

?php

class Ccust_data {
function Cform_data() {
$this-test = ;
}
}

$o = new Ccust_data();
$o-test = 1;

while ($y = 0) {
global $o-test;
$o-test = 2;
$y = 1;

}

echo \$o-test = $o-test\n;

?
-- 
Randy Perry
sysTame
Mac Consulting/Sales

phn 561.589.6449
mobile email[EMAIL PROTECTED]



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




Re: [PHP] Scope problem in while loop

2002-03-11 Thread Lars Torben Wilson

On Mon, 2002-03-11 at 18:51, Randall Perry wrote:
 According to the PHP 4 docs all variables are global unless within a
 function. I've got the following test code which should output 2, but
 outputs 1. The while loop creates it's own class object (which seems strange
 since it isn't explicitly instantiated by my code; I would think it would
 cause errors).

Actually, on PHP 4.2.0-dev, I get a parse error on the 'global $o-test;'
line (you can't globalize an object attribute).

If I remove that line, the code does indeed produce '$o-test = 1;'. 
That's because the loop never executes. Consider this change:

  while ($y = 0) {
  echo In the loop.\n;
  $o-test = 2;
  $y = 1;
  }

When executed, 'In the loop' is never printed. This is because the loop
never executes--because the while() condition is wrong. The '=' should 
be a '==' or '==='. What's happening at the moment is that the while()
condition '$y = 0' is assigning 0 to $y, not comparing 0 to $y. The 
overall value of that expression, then, is 0--which evaluates to false.
So the while loop never runs. If I change the '=' to '==', I get the 
correct output:

  Notice:  Undefined variable:  y in 
  /home/torben/public_html/phptest/__phplist.html on line 27

  In the loop.
  $o-test = 2

The notice is easily corrected by initializing $y before testing its
value. The following should work for you:

?php
error_reporting(E_ALL);

class Ccust_data {
function Cform_data() {
$this-test = ;
}
}

$o = new Ccust_data();
$o-test = 1;

$y = 0;
while ($y == 0) {
echo In the loop.\n;
$o-test = 2;
$y = 1;
}

echo \$o-test = $o-test\n;

?


What does this code do for you?

Torben

 The reason I created an object for my variable (actually, I started testing
 with regular strings) is that I've had similar scoping problems in perl,
 where variables got out of scope within loops (or even if statements). In
 perl, declaring an object outside these structures will protect it's scope.
 Not so in PHP I see.
 
 Can anyone explain this behavior? Do I have to create functions that return
 values every time I need a loop that modifies a variable?
 
 ?php
 
 class Ccust_data {
 function Cform_data() {
 $this-test = ;
 }
 }
 
 $o = new Ccust_data();
 $o-test = 1;
 
 while ($y = 0) {
 global $o-test;
 $o-test = 2;
 $y = 1;
 
 }
 
 echo \$o-test = $o-test\n;
 
 ?
 -- 
 Randy Perry
 sysTame
 Mac Consulting/Sales
 
-- 
 Torben Wilson [EMAIL PROTECTED]
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


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




RE: [PHP] Scope problem in while loop

2002-03-11 Thread Demitrious S. Kelly

I may be wrong, but that's exactly what I ended up having to do... but
don't quote me - I'm just learning OOP

http://www.apokalyptik.com/forum/viewtopic.php?topic=140forum=60

-Original Message-
From: Randall Perry [mailto:[EMAIL PROTECTED]] 
Sent: Monday, March 11, 2002 6:52 PM
To: [EMAIL PROTECTED]
Subject: [PHP] Scope problem in while loop

According to the PHP 4 docs all variables are global unless within a
function. I've got the following test code which should output 2, but
outputs 1. The while loop creates it's own class object (which seems
strange
since it isn't explicitly instantiated by my code; I would think it
would
cause errors).

The reason I created an object for my variable (actually, I started
testing
with regular strings) is that I've had similar scoping problems in perl,
where variables got out of scope within loops (or even if statements).
In
perl, declaring an object outside these structures will protect it's
scope.
Not so in PHP I see.

Can anyone explain this behavior? Do I have to create functions that
return
values every time I need a loop that modifies a variable?

?php

class Ccust_data {
function Cform_data() {
$this-test = ;
}
}

$o = new Ccust_data();
$o-test = 1;

while ($y = 0) {
global $o-test;
$o-test = 2;
$y = 1;

}

echo \$o-test = $o-test\n;

?
-- 
Randy Perry
sysTame
Mac Consulting/Sales

phn 561.589.6449
mobile email[EMAIL PROTECTED]



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





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




Re: [PHP] Scope problem in while loop

2002-03-11 Thread Randall Perry

Whoops, you're right. Classic 'C' mistake using = instead of ==. Never mind
:(

 On Mon, 2002-03-11 at 18:51, Randall Perry wrote:
 According to the PHP 4 docs all variables are global unless within a
 function. I've got the following test code which should output 2, but
 outputs 1. The while loop creates it's own class object (which seems strange
 since it isn't explicitly instantiated by my code; I would think it would
 cause errors).
 
 Actually, on PHP 4.2.0-dev, I get a parse error on the 'global $o-test;'
 line (you can't globalize an object attribute).
 
 If I remove that line, the code does indeed produce '$o-test = 1;'.
 That's because the loop never executes. Consider this change:
 
 while ($y = 0) {
 echo In the loop.\n;
 $o-test = 2;
 $y = 1;
 }
 
 When executed, 'In the loop' is never printed. This is because the loop
 never executes--because the while() condition is wrong. The '=' should
 be a '==' or '==='. What's happening at the moment is that the while()
 condition '$y = 0' is assigning 0 to $y, not comparing 0 to $y. The
 overall value of that expression, then, is 0--which evaluates to false.
 So the while loop never runs. If I change the '=' to '==', I get the
 correct output:
 
 Notice:  Undefined variable:  y in
 /home/torben/public_html/phptest/__phplist.html on line 27
 
 In the loop.
 $o-test = 2
 
 The notice is easily corrected by initializing $y before testing its
 value. The following should work for you:
 
 ?php
 error_reporting(E_ALL);
 
 class Ccust_data {
 function Cform_data() {
 $this-test = ;
 }
 }
 
 $o = new Ccust_data();
 $o-test = 1;
 
 $y = 0;
 while ($y == 0) {
 echo In the loop.\n;
 $o-test = 2;
 $y = 1;
 }
 
 echo \$o-test = $o-test\n;
 
 ?
 
 
 What does this code do for you?
 
 Torben
 
 The reason I created an object for my variable (actually, I started testing
 with regular strings) is that I've had similar scoping problems in perl,
 where variables got out of scope within loops (or even if statements). In
 perl, declaring an object outside these structures will protect it's scope.
 Not so in PHP I see.
 
 Can anyone explain this behavior? Do I have to create functions that return
 values every time I need a loop that modifies a variable?
 
 ?php
 
 class Ccust_data {
 function Cform_data() {
 $this-test = ;
 }
 }
 
 $o = new Ccust_data();
 $o-test = 1;
 
 while ($y = 0) {
 global $o-test;
 $o-test = 2;
 $y = 1;
 
 }
 
 echo \$o-test = $o-test\n;
 
 ?
 -- 
 Randy Perry
 sysTame
 Mac Consulting/Sales
 

-- 
Randy Perry
sysTame
Mac Consulting/Sales

phn 561.589.6449
mobile email[EMAIL PROTECTED]



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




[PHP] scope issue?

2002-03-10 Thread Nick Wilson

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi, 
I have the solution to my problem here but can't see why i need it?

this snippet takes $AuthId from a form and but does not work (doesn't
seem to pass it to the function)

case edit:
$tips-get_author($AuthId);
$content=edit_author_form($tips-auth_id);
break;

but this works fine...

case edit:
$id=$AuthId;
$tips-get_author($id);
$content=edit_author_form($tips-auth_id);
break;

Why?

Many thanks

- -- 
- ---
 www.explodingnet.com   |Projects, Forums and
+Articles for website owners 
- -- Nick Wilson -- |and designers.

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.6 (GNU/Linux)

iD4DBQE8i5SQHpvrrTa6L5oRAuh7AKCmdRmslIv+sexSyW+vtuad8flu0wCXYaKt
bjROMlsgU5XtiZ2ewhybJQ==
=6ZTA
-END PGP SIGNATURE-

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




[PHP] @ Scope

2001-02-14 Thread Karl J. Stubsjoen

When you @ "at" a command (supress error messaging) within a function, is
the scope of the @ within the function?

Example:


CloseODBC(1);

# is error message supressed here too?


function CloseODBC($connection_id)
{
# error messaging supressed
@odbc_close($connection_id);

}


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] @ Scope

2001-02-14 Thread Web master

My understanding is, it simple supress the any messages generated from 
the result. So I guess it is local.

Karl J. Stubsjoen wrote:

 When you @ "at" a command (supress error messaging) within a function, is
 the scope of the @ within the function?
 
 Example:
 
 
 CloseODBC(1);
 
 # is error message supressed here too?
 
 
 function CloseODBC($connection_id)
 {
 # error messaging supressed
 @odbc_close($connection_id);
 
 }
 
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] @ Scope

2001-02-14 Thread Christian Reiniger

On Wednesday 14 February 2001 17:36, Karl J. Stubsjoen wrote:
 When you @ "at" a command (supress error messaging) within a function,
 is the scope of the @ within the function?

 Example:


 CloseODBC(1);

 # is error message supressed here too?


 function CloseODBC($connection_id)
 {
 # error messaging supressed
 @odbc_close($connection_id);

 }


Huh?

When you call CloseODBC (), the code in that function is executed - and 
there odbc_close() is called with the "@" - so it doesn't report an error.

-- 
Christian Reiniger
LGDC Webmaster (http://sunsite.dk/lgdc/)

Even idiots can handle computers, and many do.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]