0

For example i have a file file1.php:

Now, i have another php file file2.php in which i am including in file1.php:

include "file1.php";

I don't want some code to include. Is there any way or function to restrict that code to include?

Amit Gupta
  • 2,771
  • 2
  • 17
  • 31
Yasir Mushtaq
  • 158
  • 1
  • 13
  • you can add a check on the particular code – Umar Majeed Jan 05 '18 at 07:27
  • Do you practice OOP (Object Oriented Programming)? that is very useful like in your case. – Jeffrey Hitosis Jan 05 '18 at 07:37
  • @OmerMuhammad ... I want to execute all code in file1.php but dont want to load some code in file2.php... If i will use a check or condition, then that code will also not execute in file1.php... and if i make that condition true in file1.php to execute... Then it will become part of file2.php too – Yasir Mushtaq Jan 05 '18 at 07:45
  • your question is not clear yet to anybody please do some research try it your self then come here to ask a solution – Umar Majeed Jan 05 '18 at 07:50

2 Answers2

0

Convert that code block to function(in first file) that you don't want to execute in second file, this will execute only if you call that function otherwise not

jDeep
  • 11
  • 2
  • Which function? – Yasir Mushtaq Jan 05 '18 at 07:20
  • i assume that your code is directly executed, so place it inside some function: `function myCode() {here your code} `. this will execute only if you call 'myCode()' function, otherwise not – jDeep Jan 05 '18 at 07:23
  • jDeep I think this is not the question or answer he is asking to don't load a particular part of code not all the code – Umar Majeed Jan 05 '18 at 07:28
  • He said **I don't want some code to include.**. so i thinks he don't want that code execution so i suggest him to convert that **some code** to function then it will not executed automatically – jDeep Jan 05 '18 at 07:31
  • @Omer Muhammad yes... I dont want to include a particular code to load in other php file when including – Yasir Mushtaq Jan 05 '18 at 07:31
  • is there any case or condition when you don't want to load a particular code like some special request etc so that you can put a check/condition to don't load – Umar Majeed Jan 05 '18 at 07:34
  • jDeep if i will convert that code in a function then file1.php will also not execute that code... I want to execute the particular code in file1.php but not in file2.php – Yasir Mushtaq Jan 05 '18 at 07:38
  • [THIS ANSWER WILL HELP YOU](https://stackoverflow.com/a/33485352/9172680) – jDeep Jan 05 '18 at 07:46
0

TRY this : In you file1.php add all code in small functions. Then call only those function in file2.php which is needed . You can also use Oops Concept create class and call needed method only .

file1.php

method1();
mehtod2();
method3();

file2.php
include file.php

method1();

Sorry : You have to use oops concept in PHP .

Amitesh Kumar
  • 3,051
  • 1
  • 26
  • 42