Php include in class function

how can i include php file in php class

You can pass it as an argument, or use the global keyword to put it in the current scope. However, using global is discouraged, try passing it as an argument.

And in your other file call this class method with your array as an argument.

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

but like others pointed out before, you dont want to do that, because it introduces a dependency on the filesystem in your class. If your method requires those variables to work, then inject them as method arguments or pass them into the constructor and make them a property (if you need them more often). This is called Dependency Injection and it will make your code much more maintainable in the long run, e.g. do

class class1 < private $data; public function __construct(array $var) < $this->data = $var; > function fnc1() < echo $this->data['var2']; //rest of function here > > 
$obj = new class1($var); echo $obj->fnc1(); 

or require the data to be passed into the method on invocation

$obj = new class1; $obj->fnc1($var); 

Источник

Access property from include inside a class method in PHP

Is this possible given the scope. If var is an array then we can use extract but if var is not, we can wrap it in an array. Is there a better way? Thanks! EDIT okay, to clarify another.php is literally another file. Basically, in the above examples, we have 2 files A.php which contains class A and another.php which is another file/script executing something. Answered: My bad. I included another.php from index.php.. I see scoping still applies.. thanks everyone..

In short, just delete line 4 of index.php and it will work. but that’s why I don’t recommend depending on this behavior: like I said in my answer, if you include another.php anywhere else, then you will get an error

3 Answers 3

Your question seems to be, «when inside a file included from within a method, how do I access a private instance member?» Right?

In your example code, you’re including a file inside a method.

Читайте также:  Разбить строку на элементы python

Methods are just functions. Like all other areas of PHP, the file that gets included will inherit the entire current scope. That means that the include sees everything in scope in that method. Including $this .

In other words, you’d access the property in the include file just like you’d access it from inside the function itself, as $this->var .

Example, using the PHP interactive shell:

[charles@lobotomy /tmp]$ cat test.php var, "\n"; [charles@lobotomy /tmp]$ php -a Interactive shell php > class Test2 < private $var; public function __construct($x) < $this->var = $x; > public function go() < include './test.php'; >> php > $t = new Test2('Hello, world!'); php > $t->go(); Hello, world! php > exit [charles@lobotomy /tmp]$ php --version PHP 5.4.4 (cli) (built: Jun 14 2012 18:31:18) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies with Xdebug v2.2.0rc1, Copyright (c) 2002-2012, by Derick Rethans 

well, i just tried it and: Fatal error: Using $this when not in object context in ****\another.php on line 3

You have defined $var as private, which means $var can only be accessed by member functions. If you need to access $var , either make it public, or return it from a member function. You should read more about visibility from the PHP Manual

Edit: What makes your situation interesting is that you are calling include from a member function. include will inherit the scope from which it is called. So, technically, you can call $this->var from another.php . However, I strongly encourage against this practice. If another.php gets included anywhere else, you will get errors. Please, please don’t do this. It’s terrible programming practice.

If you really must, add these lines to A.php :

$obj = new A(); $obj->go(); // this will call another.php, which will echo "$this->var" 

And then change another.php to this:

And it will work; you will get the right output. Note that if you do not declare an instance of class A this will fail (eg, A::go() , A->go() , etc will all fail). This is such a terrible way to go about things in PHP.

But doing things a better way, you could make the variable public:

class A < public $var = 1; //note, it is public! public function go() < include('another.php'); >> $obj = new A(); echo $obj->var; //woot! 

Or, keep it private (which is better OOP):

class A < private $var = 1; //note, it is private //make a public function that returns var: public function getVar() < return $this->var; > public function go() < include('another.php'); >> $obj = new A(); echo $obj->getVar(); //woot! 

Источник

Читайте также:  Compress css online tool

PHP include/require inside functions

Having functions that are quite big and they are loading every time the page is loaded, would be better to write function foo() < include(. /file_with_function's_code); return; >to minimize the size of the functions script? Or it doesn’t matter because when a function is loaded (but not executed) also is loaded the content even if it is into an include? Thank you. (Edit: my question is not about if it’s possible or not)

4 Answers 4

While @Luceos answer is technically correct (the best kind of correct), it does not answer the question you asked, namely is doing this better, or do includes happen regardless of function calls?

I tested this in the most basic way (OP, why didn’t you?):

 //doThing(); echo "Done testing."; 

if I call doThing(); I get a file not found warning.

If I comment out doThing(); . there is no error! So you can save file load time by doing this.

Or, as a good alternative, encapsulate your functions in classes, and take the benefit of __autoload :

function __autoload($class_name)

Encapsulate myBigFunction() in a class

save it as myBigFunction.php

When you call the function as static method on the class :

__autoload will load the file, but not before that.

This is definitely the way to go. Splitting functions into individual files will cause a lot of extra confusion, and the many small files may actually make it less performant. Grouping functions into meaningful classes, on the other hand, helps you organise your code and maintain it as it grows.

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.

Question is, why not add the surrounding function definition to that included file. I think the only viable reason to include within a function is to split code within that function into bits.

Yes, I know it is possible. My question is about optimization. Would be better to write an include inside a function in order to make it smaller? (Having functions that are quite big and they are loading every time the page is loaded). Thank you.

Both Luceos’ and Albatrosz could be misread, so I felt that I should clarify these.

Читайте также:  Php close all buffers

The include set of directives generate a runtime ZEND_INCLUDE_OR_EVAL operation which calls the Zend compiler to compile the referenced file. So in general you should not embed include statements in a function, as:

  • The include will be executed every time that code path is taken when the function is called. Compiling the same bit of code 100s of times is a bad idea.
  • If the code contains elements of global scope (e.g. function or class declarations) then executing that declaration even twice will cause compiler errors.

So don’t unless you know what you are doing. Use techniques such are those described by Albatrosz. Incidentally his __autoload() function is just the sort of example of an exception where this is valid to do.

Источник

PHP including files inside classes

I only just realised that files included inside an objects scope are inaccessable from the global scope. What is the best way to include a file inside an object so it can be accessed globally? I would like to be able to:

$loadfiles->settings(); $loadfiles->classes(); $loadfiles->passwords(); 

include is the same as cut and pasting the code from the file in a text editor. So what ever is inserted that is what will be executed as PHP source code.

Is this actual working code? If so, then you need to read up on how to instantiate objects. See the new keyword.

2 Answers 2

It doesn’t matter where you include or require code from in PHP. The interpreter is pretty linear in it’s first definition pass, that is to say that it will basically compress all of the included / required files into one large file in the exact order in how it was read.

One thing to note about this is that scope does change. but everything is applied to the «global» scope. You can always import something from the global scope into your current scope using the «global» keyword to declare a variable prior to using it. So when you want to use a «global» variable from another script just ask for it.

include('b.php'); global $myVar; echo $myVar; 

What the interpreter see’s this code as after it’s first pass

// In global scope $myVar = 'Hello World' // In a.php scope global $myVar; echo $myVar; 

In short from your php file simply add the line

After you include the crucialsettings.php file and your echo will work.

Источник

Оцените статью