Include and class PHP - 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.

Related

Stuck with php function not returning any data

I am expecting the below code to return hi when the project is run.But it's not returning anything .There is no error also.I've started the wamp server. How can i fix this?I'm very new to this. Anyone can tell me this?
functions.php
<?php
function getDate($orderDate, $oderTime) {
return "hi";
}
?>
index.php
<?php
require "vendor/autoload.php";
require "config.php";
require "functions.php";
getDate(var_export($days),var_export($Time));
config.php
<?php
$Time = "1";
$days = ["Sat", "Sun"];
when you use return the return value wil not shown on your screen till you echo it out, like this
echo getDate(var_export($days),var_export($Time));
and by the way you can replace return keyword in your function with echo
in this case you don't need to write echo getDate(var_export($days),var_export($Time));
just type getDate(var_export($days),var_export($Time));

Error while including class from anthor file in 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";

Can't create an instance of my class

i'm sorry if this is a noob question, but i can't figure it out.
I have a .php file containing a class:
<?php
class Visitors
{
public function greetVisitor()
{
echo "Hello<br />";
}
function sayGoodbye()
{
echo "Goodbye<br />";
}
}
?>
In another php file containing mostly HTML, i try to create an instance of this class and call the function greetVisitor :
<div class="map col-md-4">
<?php
$test = new Visitors();
$test->sayGoodbye();
?>
</div>
For some reason this isn't displaying when i look in my browser. Any ideas ?
tried doing a var_dump, but nothing is showing
thanks
Before you create an object of the class Visitors you need to include/require the php file where that class is defined, like this:
By the way, do not use the parenthesis since you are not sending any values
and yes you are a newbie!

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.

require_once of function file in a header

ive got an global include/header.php file like:
UPDATE
folder structure :
/
include/
header.php
functions.php
content/
show.php
include/header.php
<?php
require_once('functions.php');
$settings = blaa;
....
?>
include/functions.php
<?php
function hello()
{
echo "hello world";
}
?>
and now a content file like content/show.php
content/show.php
<?php
require_once('../include/header.php');
echo "show page want to say: ";
hello();
?>
and now if i look in the apache error_log call to undefined function in content/show.php on line...
i cannot find out why :-/
Greetings
If you tweak your header.php code to this:
<?php
define('INCDIR', str_replace('\\', '/', dirname(__FILE__)));
require_once(INCDIR . '/functions.php');
It wont matter where you include it from it will constantly find the right path to the include folder

Categories