Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Creating a class and reusing variables...
I hope this is an obvious question, but I'm very new to classes.

I have created a class that have a number of variables defined for it.

class cart {

var $username = "blah";
var $password = "blahblah";
var $database = "zippy";
var $hostname = "127.0.0.1";

Etc...

It suddenly struck me that it would make more sense to have these variables
stored in another php include so that other functions and scripts can use
these values without them being duplciated.

However I cannot find out how to pull my "global" variables into my class.

I believe I could make the values constants with

Define('USERNAME' , 'blah');

And simply use that a constant my class. But that also means going through a
whole load of code and changing my $username to USERNAME and probably
buggering something up there.

So if anyone can point the way to getting variables into a class from
"outside" I would appreciate it.


Report this thread to moderator Post Follow-up to this message
Old Post
Bonge Boo!
05-26-05 08:56 AM


Re: Creating a class and reusing variables...
Bonge Boo! wrote:
> I hope this is an obvious question, but I'm very new to classes.
>
> I have created a class that have a number of variables defined for it.
>
> class cart {
>
>     var $username = "blah";
>     var $password = "blahblah";
>     var $database = "zippy";
>     var $hostname = "127.0.0.1";
>
> Etc...
>
> It suddenly struck me that it would make more sense to have these variable
s
> stored in another php include so that other functions and scripts can use
> these values without them being duplciated.
>
> However I cannot find out how to pull my "global" variables into my class.
>
> I believe I could make the values constants with
>
> Define('USERNAME' , 'blah');
>
> And simply use that a constant my class. But that also means going through
 a
> whole load of code and changing my $username to USERNAME and probably
> buggering something up there.
>
> So if anyone can point the way to getting variables into a class from
> "outside" I would appreciate it.
>
There's a few ways to do it...  probably simplest way for you would be
to do something like this inside your class..

$var =& $GLOBALS['var']; // this will only create a reference to the
global variable $var inside your object. If you want a copy take out the
& ($var = )

hope that helps..

btw if you have any variable that you need to "change" and its too much
hasle... sometimes a good workaround is this exact "trick" ...

$oldVar =& $NEW_VAR; doesn't take up any new resources as you're only
creating a "pointer" not a duplicate ..

Report this thread to moderator Post Follow-up to this message
Old Post
Paul
05-26-05 01:56 PM


Re: Creating a class and reusing variables...
Bonge Boo! wrote:
> I hope this is an obvious question, but I'm very new to classes.
>
> I have created a class that have a number of variables defined for it.
>
> class cart {
>
>     var $username = "blah";
>     var $password = "blahblah";
>     var $database = "zippy";
>     var $hostname = "127.0.0.1";
>
> Etc...
>
> It suddenly struck me that it would make more sense to have these variable
s
> stored in another php include so that other functions and scripts can use
> these values without them being duplciated.
>
> However I cannot find out how to pull my "global" variables into my class.
>
> I believe I could make the values constants with
>
> Define('USERNAME' , 'blah');
>
> And simply use that a constant my class. But that also means going through
 a
> whole load of code and changing my $username to USERNAME and probably
> buggering something up there.
>
> So if anyone can point the way to getting variables into a class from
> "outside" I would appreciate it.
>
sorry just a quick note I caught myself on...

inside the object you'd have to declare the variable...  like so..

class Cart {
var $var;

function Cart() {
$this->var =& $GLOBALS['var'];
}

}

for php 5 you can use its "native" constructor __Construct() instead of
the shown example... which is for PHP < 5 :)

Report this thread to moderator Post Follow-up to this message
Old Post
Paul
05-26-05 01:56 PM


Re: Creating a class and reusing variables...
On 26/5/05 7:53 am, in article Gnele.1486038$8l.366880@pd7tw1no, "Paul"
<freelance@dezignage.net> wrote:
 
> sorry just a quick note I caught myself on...
>
> inside the object you'd have to declare the variable...  like so..
>
> class Cart {
>  var $var;
>
>  function Cart() {
>    $this->var =& $GLOBALS['var'];
>  }
>
> }

Ummm. Forigve the dim question. I have a lot of functions in this class. I
wouldn't need to declare them inside each function would I?

Big gulp.

If that was the case it would be easier to just do as I currently do and
define the variables inside the class explicitly. Which works beautifully,
but I'm trying to make my code as streamlined and centralised as possible,
mainly do to my own ineptness making me worry I'll lose track.



Report this thread to moderator Post Follow-up to this message
Old Post
Bonge Boo!
05-26-05 01:56 PM


Re: Creating a class and reusing variables...
On 26/5/05 7:53 am, in article Gnele.1486038$8l.366880@pd7tw1no, "Paul"
<freelance@dezignage.net> wrote:

> sorry just a quick note I caught myself on...
>
> inside the object you'd have to declare the variable...  like so..
>
> class Cart {
>  var $var;
>
>  function Cart() {
>    $this->var =& $GLOBALS['var'];
>  }
>
> }


Ok. Been playing with all this to try to get it too work. Normally with a
function I could do the following:

$foo = "apples";

function myfunction () {
extract($GLOBALS);
$var = $foo;
print $var;
}

myfunction();

Outputs apples. Which is fine and tells me that extract($GLOBALS) puts all
variables I have defined available in the function. If I try to use this

extract($GLOBALS);

Inside my class definition I get the following error.

Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or
`T_VAR' or `'}'' in /path/shoppingcart.inc.php on line 14

So I guess for some reason I can't use extract GLOBALS in a class.

When I try to use a defined constant when setting up my class variables then
it doesn't seem to stick. I would have thought the below should work.

var $password = SQL_PASSWORD;

But nothing seems to be assigned to $password.

If I try

var $password = $GLOBALS['password'];
or
var $password =& $GLOBALS['password'];

I get Parse error.

So it seems like the $GLOBAL keyword is what is causing the problem when
called inside a class. Does that make sense? It can't be that I am using
reserved keywords can it?


Report this thread to moderator Post Follow-up to this message
Old Post
Bonge Boo!
05-26-05 01:56 PM


Re: Creating a class and reusing variables...
On 26/5/05 7:53 am, in article Gnele.1486038$8l.366880@pd7tw1no, "Paul"
<freelance@dezignage.net> wrote:
 
> sorry just a quick note I caught myself on...
>
> inside the object you'd have to declare the variable...  like so..
>
> class Cart {
>  var $var;
>
>  function Cart() {
>    $this->var =& $GLOBALS['var'];
>  }
>
> }
>
> for php 5 you can use its "native" constructor __Construct() instead of
> the shown example... which is for PHP < 5 :)


Ok. Just found

http://docs.php.net/en/language.oop.html

Which explains the above quite nicely. So it looks like major re-write time.
Dunno if I can be bothered at this stage (which will almost certainly come
back and bit me in the ass)

Thanks for your thoughts.


Report this thread to moderator Post Follow-up to this message
Old Post
Bonge Boo!
05-26-05 01:56 PM


Re: Creating a class and reusing variables...
Bonge Boo! wrote:
> On 26/5/05 7:53 am, in article Gnele.1486038$8l.366880@pd7tw1no, "Paul"
> <freelance@dezignage.net> wrote:
>
> 
>
>
>
> Ok. Been playing with all this to try to get it too work. Normally with a
> function I could do the following:
>
> $foo = "apples";
>
> function myfunction () {
>     extract($GLOBALS);
>     $var = $foo;
>     print $var;
> }
>
> myfunction();
>
> Outputs apples. Which is fine and tells me that extract($GLOBALS) puts all
> variables I have defined available in the function. If I try to use this
>
>  extract($GLOBALS);
>
> Inside my class definition I get the following error.
>
> Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or
> `T_VAR' or `'}'' in /path/shoppingcart.inc.php on line 14
>
> So I guess for some reason I can't use extract GLOBALS in a class.
>
> When I try to use a defined constant when setting up my class variables th
en
> it doesn't seem to stick. I would have thought the below should work.
>
> var $password = SQL_PASSWORD;
>
> But nothing seems to be assigned to $password.
>
> If I try
>
> var $password = $GLOBALS['password'];
> or
> var $password =& $GLOBALS['password'];
>
> I get Parse error.
>
> So it seems like the $GLOBAL keyword is what is causing the problem when
> called inside a class. Does that make sense? It can't be that I am using
> reserved keywords can it?
>

I think the reason you're getting a parser error is nothing to do with
globals... you're giving it a value in the wrong place... it should be
like this...

class someClass {
var $password;

function someClass() {     (or __Construct() in PHP 5)
$this->password = $GLOBALS['password'];
}
function someOtherFunctionInsideTheClass() {
$password =& $this->password;
// or you could probably even do this..
extract($GLOBALS);
}
}

but if you need it in every function of your class, I would just add a
reference in the beginnign of each function.. and that's it.. (that
shouldn't take that long...

function anotherFunctionOfTheClass ($arg, $arg2) {
$password =& $this->password;
}

maybe this will help? :)

Report this thread to moderator Post Follow-up to this message
Old Post
Paul
05-27-05 01:58 AM


Re: Creating a class and reusing variables...
On 26/5/05 8:13 pm, in article jdple.1486825$Xk.1472227@pd7tw3no, "Paul"
<freelance@dezignage.net> wrote:
 
>
> I think the reason you're getting a parser error is nothing to do with
> globals... you're giving it a value in the wrong place... it should be
> like this...
>
> class someClass {
>  var $password;
>
>  function someClass() {     (or __Construct() in PHP 5)
>    $this->password = $GLOBALS['password'];
>  }
>  function someOtherFunctionInsideTheClass() {
>    $password =& $this->password;
>    // or you could probably even do this..
>   extract($GLOBALS);
>  }
> }
>
> but if you need it in every function of your class, I would just add a
> reference in the beginnign of each function.. and that's it.. (that
> shouldn't take that long...
>
>  function anotherFunctionOfTheClass ($arg, $arg2) {
>    $password =& $this->password;
>  }
>
> maybe this will help? :)

I understand why it doesn't work. It just doesn't look very elegant have
repetitions of the same variable declarations inside each function.

I've just bunged my variable declarations in an include, then inside the
function just have:

function myfunc() {

$bignumber = $GLOBALS['number']*2.5;
return $bignumber;
}

It works. Dunno why I didn't do that to start with. Probably I'm a bit dim.


Report this thread to moderator Post Follow-up to this message
Old Post
Bonge Boo!
05-27-05 08:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP SQL archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:32 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.