variables and classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

variables and classes

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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';
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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. :-P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can't you just globalize it?

Code: Select all

<?php
class MyClass 
{
    function myfunction()
    {
        global $globalvar;
        //
    }
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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);
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Thanks for clearing that up.
Post Reply