2

1) Suppose I have 2 php files: fileA.php, and fileB.php And I have 1 class file: myClassFile.php

in fileA.php, I need to instantiate a MyClass class in myClassFile.php:

...
myObject = new MyClass();
...

in fileB.php, I need to use the myObject object created in fileA.php.

How would I do that? Is using sessions the way to go?

$_SESSION["my_object"] = myObject;

or are there better ways? Specifically, how will it be implemented?

2) Suppose I don't use solutions similar to the above.. Will myObject in fileA.php be lost (destroyed, or freed) when the user proceeds to fileB.php? That is, will there be no more reference to myObject in fileB.php?

3
  • its my understanding that if the user leaves fileA, myObject will be lost unless you store it somewhere (session, log file, or anywhere else). now about the first question, why would you wanna do that? and why dont you create a common php file, where you can call the class and it will be available on every page. Commented Nov 6, 2011 at 11:43
  • just create a new file, call it common.php, include it on top of all your current php files (or the ones you need the class to be available on) and inside the common.php file, include the class, instantiate it and then you can access it from all the files. Commented Nov 6, 2011 at 11:50
  • oh I also forgot, as @randomwhitetrash mentioned you can use singleton, or you can change your class get and set method to store stuff in a database or a file. Commented Nov 6, 2011 at 11:52

2 Answers 2

1

It sounds like you're doing something unnecessary complicated.

The information you passed and treated in MyClass has to be available somehow in the object.

Create a method to extract all these information to an array and save this array in a session. When reaching the next page, create an instance of MyClass again and feed the object in another method for this purpose with the information array from your session.

Sign up to request clarification or add additional context in comments.

Comments

0

I am not an expert but if your object is like a configuration object which you need to access from many places then consider using Singleton pattern

Comments

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.