Page 1 of 1
variables and classes
Posted: Mon May 22, 2006 10:50 pm
by s.dot
I have a moderator array full of moderator ids for my forums that I include at the top of the forum pages (not inside the forum class).
Code: Select all
$modsadmins = array(1169,13,27,459);
and inside my forum class I need to access this variable using in array
Code: Select all
if(($user != $postauthor) && !in_array($user,$modsadmins)){
die('Invalid Post ID.');
}
And I get the following error:
Code: Select all
Notice: Undefined variable: modsadmins in /home/smp/public_html/class/forum.class.php on line 339
Warning: in_array(): Wrong datatype for second argument in /home/smp/public_html/class/forum.class.php on line 339
How can I make this variable available inside the class?
Posted: Mon May 22, 2006 10:52 pm
by Burrito
you could make it a property of your object.
or if you just want to use a single method, you could call it statically and pass it as an argument to the method.
Posted: Mon May 22, 2006 10:56 pm
by s.dot
hmm =/
I'm not quite sure what that means.
My object would be when I call
$forum = new forum();
So how do I add that as a property?
Posted: Mon May 22, 2006 10:58 pm
by feyd
in the class definition:
Code: Select all
class classname
{
var $property;
function classname()
{
$this->property = 'something';
}
}
or in the instance:
Code: Select all
$foo = new forum();
$foo->property = 'something';
Posted: Mon May 22, 2006 11:09 pm
by s.dot
OK.
So I have the following code
Code: Select all
$forum = new forum();
$forum->modsadmins = $modsadmins; // this variable is available in this script, so its valid
and in the class I have
Code: Select all
<?php
class forum {
var $modsadmins;
and I still get the error. So I'm thinking the property can't have the same name?
Posted: Mon May 22, 2006 11:50 pm
by bdlang
The object property can absolutely have the same name as the variable in your script, it has no relationship whatsoever. Even if you utilized it within a class method the scope keeps them apart.
Generally speaking you don't want code like this:
Code: Select all
$O= &new Object();
$O->property = 'my name';
This breaks encapsulation and encourages properties to be modified at any given point outside the class. Obviously if you're still working with PHP4 you don't have a choice other than to use setters and getters and encourage everyone working with the class to use them.
At this point, are we still wrestling with the 'undefined variable' error? If you've made
modsadmins an object property, are you setting it and accessing it within the class as
$this->modsadmins? Can you show us the bit of code where you're using the variable inside the class?
Posted: Tue May 23, 2006 12:22 am
by s.dot
bdlang wrote:At this point, are we still wrestling with the 'undefined variable' error? If you've made modsadmins an object property, are you setting it and accessing it within the class as $this->modsadmins?
I was still accessing it as $modsadmins. Then I tried $this->modsadmin and the script worked.
Thanks burrito, feyd, and bdlang. I have now written my first object property. Everyone applaud.

Posted: Tue May 23, 2006 12:23 am
by RobertGonzalez
Can't you just globalize it?
Code: Select all
<?php
class MyClass
{
function myfunction()
{
global $globalvar;
//
}
}
?>
Posted: Tue May 23, 2006 12:43 am
by Christopher
Everah wrote:Can't you just globalize it?
Noooooooooooooooooooooooooooooo!
It is often good to put an interface on it, that way you can add checking and initialization to it later without having to change the code that uses the object. It can either be in the constructor or with a setModAdmins() method. For example you could do something like this so the forum object defaults to a standard set of admin if no custom set is given. The goal is that an object is alway valid.
Code: Select all
class forum {
var $modsadmins;
function forum($modadmins=null) {
$this->setModAdmins($modadmins);
}
function setModAdmins($modadmins=null) {
if ($modadmins) {
$this->modsadmins = $modadmins;
} else {
$this->modsadmins = array(13,27,42); // standard admins
}
}
}
$forum = new forum($modsadmins);
Posted: Tue May 23, 2006 1:50 am
by RobertGonzalez
Thanks for clearing that up.