Alihan Pehlivan
Alihan Pehlivan

Reputation: 33

PHP Class - Call array variable

I am stuck part of this code.. I tried to call my static table variables but I can't.

Methinks I have very simple problem but.. effective and harmful for my project.

my class:

<?php

public $table;

class Functions
{
    public function ehe()
    {
        return $table[4];
    }
}

?>

my system.php file:

    $fn = new Functions();

    /* static tables */
    $table  = array(
                    0 => 'account.account',
                    1 => 'player.player',
                    2 => 'auction_house.items',
                    3 => 'auction_house.admin',
                    4 => 'auction_house.store',
                    5 => 'auction_house.styles',
                    6 => 'auction_house.logs',
                    7 => 'auction_house.coupons',
                    );

my index file(example): (connected with system.php & classes)

echo $fn->ehe;

my error:

Notice: Undefined property: Functions::$ehe in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\.......php on line 178

A Little second question: Also I have 2 classes. 1: Functions class. (Include query processings etc.) 2: MySQL connect class. I want to connect this two classes for queries. How is this possible? ..

Upvotes: 2

Views: 2054

Answers (2)

George Brighton
George Brighton

Reputation: 5151

There are several problems with your code:

  • You're accessing ehe as if it were a property rather than a method. Method calls always have (), which contain arguments if necessary.
  • You can't modify the visibility of variables in the global scope - your public $table; will cause a syntax error.
  • $table is out of scope in your ehe() method.

You could potentially use the global keyword:

$table;

class Functions {
    public function ehe() {
        global $table;
        return $table[4];
    }
}

$fn = new Functions();

/* static tables */
$table = array(
            0 => 'account.account',
            1 => 'player.player',
            2 => 'auction_house.items',
            3 => 'auction_house.admin',
            4 => 'auction_house.store',
            5 => 'auction_house.styles',
            6 => 'auction_house.logs',
            7 => 'auction_house.coupons',
        );

echo $fn->ehe(); // auction_house.store

Upvotes: 0

Zwirbelbart
Zwirbelbart

Reputation: 827

You need to call it, it's a function, not a property.

echo $fn->ehe();

Upvotes: 1

Related Questions