Error while including class from anthor file in php - php

I want to include a class from file own.php but I am not able to include it as it giving me error as require(class.own.php): failed to open stream: No such file or directory .
I tried all the options i.e include, require, require_once but then also it showing me a error.
include("class/own.php");
own.php
<?php
class own{
public function title(){
$title = $_POST['title'];
echo $title;
}
}
?>
display.php
include("class/own.php");
$obj = new own;
$obj->title();

Your directory structure should be like this
display.php
class/own.php
Then try to include
include("class/own.php");
The use
$obj = new own(); OR $obj = new \own();
$obj->title();

use this:
<?php
class own {
function title() {
$this->model = $_POST['title'];
}
}
// create an object
$test = new own();
// show object properties
echo $test->model;
?>

I think the two files are in the same path (directory), so you are including wrong path. It might be include("own.php");
Considering the following directory structure.
|directory
display.php
own.php
display.php
<?php
include("own.php");
$obj = new own;
$obj->title();
?>

I think you are confusing PHP. To include the file the following code may help
include (__DIR__)."/own.php";

Related

PHP Variable to Handle Web Properties

i look around the web for a example of manager PHP Variable think on properties of the web application .
the main problem is that i don't like to write igual that this code:
Link
I would like to include a file .php With class; up with the list of variables, something like this:
$MSGdisplay = '';
$MSGemail = '';
$MSGnotification = '';
and use them anywhere in the script with a simple call:
$G['MSGdisplay'] = 'This is an example of code'; //more short that $_GLOBALS array
without losing the ability to assign new values.
Example:
file index.php
<?php
require_once("main.php");
global $G;
$G['test'] = 'Text Test';
$WebAPP = new Class_MAIN();
$WebAPP -> Main();
?>
file Main.php
<?php
class Class_MAIN{
function Main(){
echo $G['test'];
}
}
?>
Notice: Undefined variable: G in main.php on line 4
Remove global $G; from index.php and move it inside of the Main function in Main.php
<?php
class Class_MAIN{
function Main(){
global $G;
echo $G['test'];
}
}
?>
can solve create a file vars.php with content variable:
<?php
$G['test'] = 'text test';
?>
an create a callback file varscall.php with content variable call glabal
<?php
global $G;
?>
Use Require instance file in Index on top:
<?php
require("vars.php");
require_once("main.php");
$WebAPP = new Class_MAIN();
$WebAPP -> Main();
?>
Use CallBack Vars File igual and use variable inside the function you need:
<?php
class Class_MAIN{
function Main(){
require("varscall.php");
echo $G['test'];
}
function Other(){
require("varscall.php");
echo $G['test'];
}
}
?>
The main problem is that you need repeat per funcion the require of the file call: require("varscall.php"); and need create this as object... is not eficient

How can I make a require_once header file in php to be used inside functions?

Can I set it as a global variable like:
<?php
$GLOBALS['dbconnect'] = require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR.'/location/file.php');
$short = $GLOBALS['dbconnect'];
function someFunction() {
echo $short;
}
?>
I am using a database connection file twice, once outside of a function, and once inside a function. The query inside the function can't run because the credentials, servername, db, etc.. are not defined.
I'm not sure how this works?
When I place the require_once file inside the brackets, nothing happens, page is white.
This is the first example from link
<?php
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
If you don't want to use an auto loader for whatever reason you can do the following. Have a file called config.php at the root of your project and have it contain this code.
<?php
// Replaced the \ which appear on localhost to / so it works online.
define("BASE_DIR", str_replace("\\", "/", __DIR__));
$files = [
BASE_DIR . "/path/to/my/file.php"
];
function loadFiles()
{
foreach ($files as $file) {
require_once $file;
}
}
?>
Then in your other files include the config file and call loadFiles. This is essentially an autoloader but sometimes it can be hard to grasp so you can use this.

Include and class PHP

Guys why doesn't php read my class file? I have this structure:
home.php
stamp.php
class/class.stamp.php
class/class.text.php
home.php
<?php
echo "hello i'm the home page";
include 'class/class.stamp.php'
$stamp = new Stamp();
include 'class/class.text.php';
?>
class.text.php
<?php
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text);
echo $text;
}
}
?>
The result? Nothing! It says:
Fatal error: Call to a member function something on a non-object in /bla/bla/bla/
But if i do something like that, it works!
home.php
<?php
echo "hello i'm the home page";
include 'class/class.text.php';
?>
class.text.php
<?php
include 'class.stamp.php'
$stamp = new Stamp();
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text) {
echo $text;
}
}
?>
But I don't need that, please guys help me :(
Check if the issue is with the directory.
You said it works with class.stamp.php, but shouldn't that be class/class.stamp.php? Here's the thing, if your homepage is located in a certain folder, then in that same folder location you should have the folder class, adjacent to your Home.php file.
I've also had the issue of uninstantiated object, so make sure you are including it in every scope. To help you catch the error, use
require "class/class.stamp.php";//Throws an error if it can't find the file.
require_once "class/class.stamp.php";//Bad practice
If require doesn't help you solve it, post everything please.

PHP Fatal error: Class 'FacebookAds\Object\AdAccount' not found in

I am using Facebook Ads API to pull data from ads Reporting.
Below is my code :
<?php
use FacebookAds\Object\AdAccount;
$account = new AdAccount('act_xxxx');
$params = array(
'date_preset'=>'last_28_days',
'data_columns'=>"['adgroup_id']",
);
$stats = $account->getReportsStats(null, $params);
foreach($stats as $stat) {
echo "is it inside the foreach loop \n";
echo $stat->impressions;
echo $stat->actions;
}
?>
I get FacebookAds/Object/AdAccount not found. I checked the path and everything looks correct. any idea, what could be the reason for this error. I am not a PHP Expert, so please do correct me, if something is wrong with my code.
<?php
function __autoload($class) {
require_once $class.".php";
}
Save this file as autoload.php in same directory then add below code at start
<?php
require_once('./autoload.php');
Explanation:
In your code you haven't included the file which contains the class FacebookAds\Object\AdAccount. That's why it gives class not found error.
Above code will make sure that all necessary class files are include in code.

How aright use globals elements

Good day.
I have problems when i use globals element in some files which include in one file.
Structure my files you can see down:
Files:
-index.php
--function.php
--globals.php
--lang.php
--allfunction.php
Code all pages you can see down:
Code index.php:
<?
session_start();
require_once("./function.php");
select();
?>
Code function.php:
<?php
require_once("./globals.php");
require_once(dirname(__FILE__)."/lang.php");
include_once Language(3);
require_once(dirname(__FILE__)."/allfunction.php");
?>
Code globals.php:
<?
$dirang = './';
$langfile = 'lang.php';
$test = 'hello';
}
?>
Code lang.php:
<?
Language($rem){
return $GLOBALS["langfile"]; //ex.
}
?>
Code allfunction.php:
<?
echo $GLOBALS["test"]; //ex.
}
?>
I get problrem when i use $GLOBALS["test"] in allfunction.php.
I get error Undefened index test in allfunction.php on line ....
Tell me please why i get it error and how aright use global element in allfunction.php ?
you should write globals inside file where you use theys, for ex. if globals you use in allfunction.php you must write global variables in this file(in which you use them)
Not use a separate file with global variables.
Don't use globals. Store your variables in static Config or Registry class instead. Also have a read about singleton design pattern.
Example:
config.php
<?php
class Config
{
static $var1 = '...';
static $var2 = '...';
public static function init()
{
self::$var2 = 1+1; //expressions go here
}
}
Config::init();
usage.php
<?php
require_once 'config.php';
function x()
{
$someKindOfSetting = Config::$var1;
}

Categories