473,569 Members | 2,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can i have an array as a class variable?

hey there, is this ok?

class MyClass
{
var $start;
var $finish;
function MyClass($start, $finish)
{
$this->start = $start;
$this->finish=$finish ,
$this->sensor_array = get_sensor_arra y();
}
function get_sensor_arra y
{
so some stuff to populate $this->sensor_array ;
return array($this->sensor_array );
}
}

another question i have,
why do i need to declare variables with the var if i can declare them
in the constructor without it ?

just a couple of questions.
if you have read this far, thanks for your time

sk

Sep 5 '06 #1
8 1768
Hey nephish,

You have some very good questions. I recently had some of these as
well.

class MyClass
{
var $start;
var $finish;
You don't HAVE to declair these variables here. It's just good
practice to do so as you can change the scope of the variables here.
function MyClass($start, $finish)
If you're using PHP 5, I'd HIGHLY recommend that you use the following
instead of what you are using here:

function __construct($st art,$finish)

Why? Because the method of building a construct using the same name as
the class itself it old-school PHP 4 stuff. If you're still using PHP
4, then by all means, keep at it. The "__construc t" method is the new
method for PHP 5. Just cleans things up a bit! Give it a try. :)
{
$this->start = $start;
$this->finish=$finish ,
$this->sensor_array = get_sensor_arra y();
}
I see no reason why this should not work. In a recent post here in
this same group, I posted a very similar example for a Database
instantiation class:

function __construct() {

$db['host'] = "localhost" ;
$db['user'] = "root";
$db['pass'] = "";
$db['name'] = "pbtportal" ;

$link = mysql_connect($ db['host'],$db['user'],$db['pass']);
mysql_select_db ($db['name'],$link);

}

Works like a charm! In this case, I did not declair the variable "$db"
but probably should have.
function get_sensor_arra y
{
so some stuff to populate $this->sensor_array ;
return array($this->sensor_array );
}
}
I hope that helps!

Sep 5 '06 #2

Slant wrote:
Hey nephish,

You have some very good questions. I recently had some of these as
well.

class MyClass
{
var $start;
var $finish;

You don't HAVE to declair these variables here. It's just good
practice to do so as you can change the scope of the variables here.
function MyClass($start, $finish)

If you're using PHP 5, I'd HIGHLY recommend that you use the following
instead of what you are using here:

function __construct($st art,$finish)

Why? Because the method of building a construct using the same name as
the class itself it old-school PHP 4 stuff. If you're still using PHP
4, then by all means, keep at it. The "__construc t" method is the new
method for PHP 5. Just cleans things up a bit! Give it a try. :)
{
$this->start = $start;
$this->finish=$finish ,
$this->sensor_array = get_sensor_arra y();
}

I see no reason why this should not work. In a recent post here in
this same group, I posted a very similar example for a Database
instantiation class:

function __construct() {

$db['host'] = "localhost" ;
$db['user'] = "root";
$db['pass'] = "";
$db['name'] = "pbtportal" ;

$link = mysql_connect($ db['host'],$db['user'],$db['pass']);
mysql_select_db ($db['name'],$link);

}

Works like a charm! In this case, I did not declair the variable "$db"
but probably should have.
function get_sensor_arra y
{
so some stuff to populate $this->sensor_array ;
return array($this->sensor_array );
}
}

I hope that helps!
Yes, this helps!
thanks very much for your reply.
i am testing code on a system with php5 and the production machine is
php4. I know this is not the best idea, but it's what i have.
i am using something similar to your code to have my user name and
password out of the web folder. It just hasn't been in a class. cool.
thanks again.
sk

Sep 5 '06 #3
"Slant" <rc****@gmail.c omwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.com.. .
I see no reason why this should not work. In a recent post here in
this same group, I posted a very similar example for a Database
instantiation class:

function __construct() {

$db['host'] = "localhost" ;
$db['user'] = "root";
$db['pass'] = "";
$db['name'] = "pbtportal" ;

$link = mysql_connect($ db['host'],$db['user'],$db['pass']);
mysql_select_db ($db['name'],$link);

}

Works like a charm! In this case, I did not declair the variable "$db"
but probably should have.
IMHO, those aren't class members, the $db elements. You should be using the
$this pointer: $this->db[...]. Now they're only visible inside the scope of
the __construct method. This is't exactly the same thing that nephish was
asking...

Still, the way nephish was doing it works fine, your example just doesn't
demonstrate it very well.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net || Gedoon-S @ IRCnet || rot13(xv***@bhg byrzcv.arg)
Sep 6 '06 #4

Kimmo Laine wrote:
"Slant" <rc****@gmail.c omwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.com.. .
I see no reason why this should not work. In a recent post here in
this same group, I posted a very similar example for a Database
instantiation class:

function __construct() {

$db['host'] = "localhost" ;
$db['user'] = "root";
$db['pass'] = "";
$db['name'] = "pbtportal" ;

$link = mysql_connect($ db['host'],$db['user'],$db['pass']);
mysql_select_db ($db['name'],$link);

}

Works like a charm! In this case, I did not declair the variable "$db"
but probably should have.

IMHO, those aren't class members, the $db elements. You should be using the
$this pointer: $this->db[...]. Now they're only visible inside the scope of
the __construct method. This is't exactly the same thing that nephish was
asking...

Still, the way nephish was doing it works fine, your example just doesn't
demonstrate it very well.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net || Gedoon-S @ IRCnet || rot13(xv***@bhg byrzcv.arg)
OK, i have a new question now.
can i set a class object in a session variable ?

like this:

class Person
{
function Person($name)
{
$this->name = $name;
}
}

$frank = new Person;
$_SESSION['name'] = $frank;

or is this just too much ?
}

Sep 6 '06 #5
Fair enough. I did not mean to be unclear. I actually did not use the
$this-specifier BECAUSE I did not want to be able to access this
array anywhere but within the construct. Not defining a variable with
$this-attached disallows other methods within the class from
accessing it, does it not? It certainly does not work anyway. Maybe
I'm missing something.

To your question though, nephish. I don't see a problem with what you
are doing. the Session variable, after all, is just another variable.
You can store many things in variables, as you obviously know. What
you're suggesting would work most efficiently if a single value was
returned from the object that the session variable is being assigned
to. For example:
$frank = new Person;
$_SESSION['auth'] = $frank->validate('fran k');

class frank {
function validate($name) {
if ($name == auth (whatever method you choose to use for
authorization)) {
return true;
} else {
return false;
}
}
}

if ($_SESSION['auth'] == true) {
echo "Yea! Fun to ensue!";
}
I'd probably not place the contents of the class directly into the
Session variable, but instead call the object to do whatever you want,
then return the data in an instance variable and assign THAT to the
Session variable. How does that sound?
$frank = new Person;
$frank->validate('fran k');
$_SESSION['auth'] = $frank->authorized;

class frank {
function validate($name) {
if ($name == authorized (whatever method you choose to use for
authorization)) {
$this->authorized = true;
} else {
$this->authorized = false;
}
}
}

if ($_SESSION['auth'] == true) {
echo "Yea! Fun to ensue!";
}
Kimmo Laine wrote:
"Slant" <rc****@gmail.c omwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.com.. .
I see no reason why this should not work. In a recent post here in
this same group, I posted a very similar example for a Database
instantiation class:

function __construct() {

$db['host'] = "localhost" ;
$db['user'] = "root";
$db['pass'] = "";
$db['name'] = "pbtportal" ;

$link = mysql_connect($ db['host'],$db['user'],$db['pass']);
mysql_select_db ($db['name'],$link);

}

Works like a charm! In this case, I did not declair the variable "$db"
but probably should have.

IMHO, those aren't class members, the $db elements. You should be using the
$this pointer: $this->db[...]. Now they're only visible inside the scope of
the __construct method. This is't exactly the same thing that nephish was
asking...

Still, the way nephish was doing it works fine, your example just doesn't
demonstrate it very well.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net || Gedoon-S @ IRCnet || rot13(xv***@bhg byrzcv.arg)
Sep 6 '06 #6

Slant wrote:
Fair enough. I did not mean to be unclear. I actually did not use the
$this-specifier BECAUSE I did not want to be able to access this
array anywhere but within the construct. Not defining a variable with
$this-attached disallows other methods within the class from
accessing it, does it not? It certainly does not work anyway. Maybe
I'm missing something.

To your question though, nephish. I don't see a problem with what you
are doing. the Session variable, after all, is just another variable.
You can store many things in variables, as you obviously know. What
you're suggesting would work most efficiently if a single value was
returned from the object that the session variable is being assigned
to. For example:
$frank = new Person;
$_SESSION['auth'] = $frank->validate('fran k');

class frank {
function validate($name) {
if ($name == auth (whatever method you choose to use for
authorization)) {
return true;
} else {
return false;
}
}
}

if ($_SESSION['auth'] == true) {
echo "Yea! Fun to ensue!";
}
I'd probably not place the contents of the class directly into the
Session variable, but instead call the object to do whatever you want,
then return the data in an instance variable and assign THAT to the
Session variable. How does that sound?
$frank = new Person;
$frank->validate('fran k');
$_SESSION['auth'] = $frank->authorized;

class frank {
function validate($name) {
if ($name == authorized (whatever method you choose to use for
authorization)) {
$this->authorized = true;
} else {
$this->authorized = false;
}
}
}

if ($_SESSION['auth'] == true) {
echo "Yea! Fun to ensue!";
}
Kimmo Laine wrote:
"Slant" <rc****@gmail.c omwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.com.. .
I see no reason why this should not work. In a recent post here in
this same group, I posted a very similar example for a Database
instantiation class:
>
function __construct() {
>
$db['host'] = "localhost" ;
$db['user'] = "root";
$db['pass'] = "";
$db['name'] = "pbtportal" ;
>
$link = mysql_connect($ db['host'],$db['user'],$db['pass']);
mysql_select_db ($db['name'],$link);
>
}
>
Works like a charm! In this case, I did not declair the variable "$db"
but probably should have.
IMHO, those aren't class members, the $db elements. You should be usingthe
$this pointer: $this->db[...]. Now they're only visible inside the scope of
the __construct method. This is't exactly the same thing that nephish was
asking...

Still, the way nephish was doing it works fine, your example just doesn't
demonstrate it very well.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net || Gedoon-S @ IRCnet || rot13(xv***@bhg byrzcv.arg)
ok, i get you, yeah, i think thats cleaner too.
thanks for your time.
nephish

Sep 6 '06 #7
"nephish" <ne*****@gmail. comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
OK, i have a new question now.
can i set a class object in a session variable ?

like this:

class Person
{
function Person($name)
{
$this->name = $name;
}
}

$frank = new Person;
$_SESSION['name'] = $frank;

or is this just too much ?
}
Not just like that, you must first convert the object to a storeable format,
it's called serializing. There is a function called serialize() and it's
compliment function unserialize(). When you store the object to session, you
serialize it first. then when you need to revert it back from the session,
you unserialize it. And also, the class definition must be available on each
page where the object is accessed. If it is missing, the object can't be
unserialized.

on page1.php:
$_SESSION['name'] = serialize($fran k);

on page2.php:
$frank = unserialize($_S ESSION['name']);

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net || Gedoon-S @ IRCnet || rot13(xv***@bhg byrzcv.arg)

Sep 7 '06 #8

Kimmo Laine wrote:
"nephish" <ne*****@gmail. comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
OK, i have a new question now.
can i set a class object in a session variable ?

like this:

class Person
{
function Person($name)
{
$this->name = $name;
}
}

$frank = new Person;
$_SESSION['name'] = $frank;

or is this just too much ?
}

Not just like that, you must first convert the object to a storeable format,
it's called serializing. There is a function called serialize() and it's
compliment function unserialize(). When you store the object to session, you
serialize it first. then when you need to revert it back from the session,
you unserialize it. And also, the class definition must be available on each
page where the object is accessed. If it is missing, the object can't be
unserialized.

on page1.php:
$_SESSION['name'] = serialize($fran k);

on page2.php:
$frank = unserialize($_S ESSION['name']);

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net || Gedoon-S @ IRCnet || rot13(xv***@bhg byrzcv.arg)
Well, thanks ! this could make my sessions and keeping track of state a
whole lot easier. I had looked at serialize, but i thought that it had
to be set in a database. I didn't know it was that easy.

thanks a lot.

shawn

Sep 7 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
14011
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
7
3196
by: Magnus Warker | last post by:
Hi, I want to traverse an (associative) array, starting from its current position, until a certain element is reached. Then, in certain cases, I want to be able to reset the current position of the array to the one that we had before touching the array. I. e., i need something like funcctions getpos and setpos: ----------
1
4208
by: jimbo | last post by:
Here is my problem. I'm creating an Instrumentation class that will use previously created Performance Categories and Counters in order to time various processes (ie. query duration etc.). This Instrumentation class will be used by a variety of "services", so the Categories and Counters to be used within the object must be set during object...
17
3005
by: yinglcs | last post by:
Hi, In STL, can I create a variable size but non-growable array? In Java, I can do this: int void f (int size) { int array = new int; // do something with array return array;
6
1879
by: eb | last post by:
Hello, This *should* be simple, but I'm not knowledgeable enough so as to get it by myself. Suppose I want a double array of a custom class (say MyTile, to tile a square). How would I declare it in MyTiledSquare.h ?
19
2420
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; //every class may have this statement self.hello = function() {
1
6831
by: phjones | last post by:
This is not a class project.The program below is to display mortgage interest paid for each payment over the term of the loan and loan balance.It is program using array. However, I am receiving the following error message: --------------------Configuration: <Default>-------------------- C:\Program Files\Xinox...
5
2493
by: Stephen3776 | last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is another way? /* * Main.java * * Created on April 29, 2007, 6:57 PM * * To change this template, choose Tools | Template Manager * and...
3
4728
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not even sure if the way I've written this is going to work but anyway, I keep getting a compilation error that says: C:\Documents and...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.