1

I want to use variables in another php file as the class. But I get always the error: Notice: Undefined variable:...

First: I create a user object: File: index.php

<?php

// include the configs / constants for the db connection
require_once("config/config.php");

// load the user class
require_once("classes/User.php");

$user = new User();

include("views/order.php");

File: User.php

class User
{
   public $color = "green";
}

File livesearch.php

require_once("../classes/User.php");

echo $User->color;

I create an object from the class user in a index.php file, I use there also a require once to the User.php file and it works. Why I cant access the variable of the class?

4
  • $User::color. $User->color implies that you've done $User = new User() beforehand. Commented Oct 10, 2013 at 14:55
  • I did it in the index.php and include a view. When the page is loading, the index.php creates a object of user and after the it shows the "real" page (view) Commented Oct 10, 2013 at 14:58
  • Why should liveseach.php should have access to a object instance which you have created in index.php? (or did I missed a require?) Commented Oct 10, 2013 at 15:06
  • the livesearch.php need some variables to customize a sql query. When the user is typing something in an input field, the livesearch needs the variables to set the "where"-filter for operating system, comapany etc. We have a database with software, which is approved for specific users or companies. This is the reason why the livesearch.php need some variables which is set with the call in the index.php. This was only a example, I dont want to post my hole code, this would blow the page. Commented Oct 10, 2013 at 15:15

3 Answers 3

4

Variable names in PHP are case sensitive:

echo $User->color;

should be

echo $user->color;

Also the livesearch.php doesn't have access to the variables in index.php unless:

  • It is includes in index.php. In which case it has access to all the variables assigned in index.php before it was included.
  • livesearch.php includes index.php. In which case it has access to all the variables assigned in index.php after the point where index.php was included.

eg. Your files, but slightly modified:

File: index.php

// load the user class
require_once("User.php");

$user = new User();

include("livesearch.php");

File: User.php

class User
{
   public $color = "green";
}

File: livesearch.php

echo $User->color;

Is the same as writing:

// From User.php
class User
{
   public $color = "green";
}

// From index.php
$user = new User();

// From livesearch.php
echo $User->color;
Sign up to request clarification or add additional context in comments.

3 Comments

that means, the variables in the User.php file can I only access, when I include the file, where the Object was created? I thought I have to include the User.php file, cause there are the variables declared.
When I include the index.php in the livesearch.php, I can access the variable. But that sucks, cause the index.php include the order.php and this is a html site. So I habe a website in a website. But I think I got it. Thanks!
@AdamRivers I got the concept. What I would like to understand and I can't find an answer to it is: If you have to include the page where you would like to use this variable wouldn't it be impracticable to include 10+ files? Is there any convinient way of doing this?
0

PHP for File livesearch.php :

 require_once("../classes/User.php");

 $user = new User;
 echo $user->color;

Comments

0

you should to use singleton of design patterns for speed. I do not recommend such a usage in this case. ( this->color instead user::color ).

research design pattern, polymorphism.

answer

  • userclass.php class User { public $color = "green"; }

$User = new User;

  • livesearch.php require_once("../classes/User.php");

echo $User->color;

1 Comment

You shouldn't use "singleton of design patterns for speed". You should use them because they suit the scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.