Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2007
    Location
    Australia - Melbourne
    Posts
    310
    Plugin Contributions
    6

    red flag Advanced Question - Class cannot store variable

    Hi, I created this class in admin/includes/classes:
    Code:
        class Designer{
            var $attributes, $values;
            
            function Designer($values = "") {
            $attributes = explode(",",DB_DESIGNERS_ATTRIBUTES);
            print_r($attributes);
                if (!is_array($values)) {
                    $values = str_replace(" ", "", $values);
                    $values = explode(",", $values);
                }
                $this->values = $values;
                return true;
            }
            
            function getDesignerAttributes() {
                echo $attributes;
                print_r($attributes);
                return $attributes;
            }
            ...
        }
    And I am trying to use that class in my own page, say admin/gallery.php:
    Code:
    $new_designer = new Designer($values);
    $a = $new_designer->getDesignerAttributes();
    echo($a);
    print_r($a);
    Why even though I have written this colored codes, this colored commands didn't return any value, but this colored commands return the correct values.

    I hope people from the development team can help with this, and I need this help kinda fast, so pleeease if you have any reply, don't hesitate ok...

    thanks guys

  2. #2
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Advanced Question - Class cannot store variable

    The $attributes variable inside each function(method) is considered "private", but you're trying to call it publicly.

    Inside each of your functions, reference $attributes as $this->attributes instead. That uses the class's $attributes variable instead of the private one inside the function.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  3. #3
    Join Date
    Jun 2007
    Location
    Australia - Melbourne
    Posts
    310
    Plugin Contributions
    6

    Default Re: Advanced Question - Class cannot store variable

    OK got it, instead of using $attributes I use $this->attributes and it works!

    Maybe because in strict writing mode, PHP doesn't accept that?

  4. #4
    Join Date
    Jun 2007
    Location
    Australia - Melbourne
    Posts
    310
    Plugin Contributions
    6

    Default Re: Advanced Question - Class cannot store variable

    Thanks a lot Dr. Byte ure my hero

  5. #5
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Advanced Question - Class cannot store variable

    Quote Originally Posted by jaycode View Post
    OK got it, instead of using $attributes I use $this->attributes and it works!

    Maybe because in strict writing mode, PHP doesn't accept that?
    That's the way PHP works. Variables are local in scope unless declared global. And declaring global is not the preferred approach.
    Within a class (or object), you refer to the class's(object's) variables via $this->; otherwise you'd need to pass variables back and forth between methods using parameters ... which isn't always the right thing to do either, depending on your needs.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

 

 

Similar Threads

  1. v151 paypaldp.php: Invalid class variable
    By lat9 in forum Bug Reports
    Replies: 0
    Last Post: 27 May 2013, 07:41 PM
  2. Please how do I get to a variable in a class
    By Dayo in forum General Questions
    Replies: 1
    Last Post: 21 Mar 2008, 02:53 PM
  3. Fatal error: Cannot redeclare class table in ...store/includes/
    By Kansai_mike in forum Built-in Shipping and Payment Modules
    Replies: 4
    Last Post: 15 Dec 2006, 06:35 AM
  4. links manager advanced - infobox class question
    By wasana in forum All Other Contributions/Addons
    Replies: 2
    Last Post: 22 Jun 2006, 04:27 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR