In my main page (index.php) I've included "class_lib.php" which contains the class definitions.
When calling the class from index.php - I'm getting the followin
Fatal error: Uncaught Error: Class 'person' not found in /home/latingate/public_html/test/ObejectOriented/index.php:5 Stack trace: #0 {main} thrown in /home/latingate/public_html/test/ObejectOriented/index.php on line 5
What am I doing wrong?
index.php
<?php include("class_lib.php"); ?>
<?php
$stefan = new person();
$jimmy = new person;
$stefan->set_name("Stefan Mischook");
$jimmy->set_name("Nick Waddles");
echo "Stefan's full name: " . $stefan->get_name();
echo "Nick's full name: " . $jimmy->get_name();
?>
class_lib.php
<?php
class person {
var $name;
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
you can see it online here:
view online
Your code is correct and works for me.
I think Problems in file Path. So, first check your class_lib.php file is not in same directory then assign correct path.
Related
How can I instatiate athe class OM inside the MySQL_DataMapper?
Here is the MySQL_DataMapper.php:
<?php
namespace asc {
include('../../models/OM.php');
class MySQL_DataMapper
{
[omitted code for simplicity]
public function fetchAllOMs()
{
$query = "REALLY LONG QUERY";
$result = $this->pdo->query($query);
$OMs = array();
while ($row = $result->fetch()){
$OM = new OM($row['id_organizacao_militar'], $row['nome'], $row['sigla'], $row['forca_armada']);
array_push($OMs, );
}
var_dump($OMs);
}
}
}
Here is the error I get:
Warning: include(../../models/OM.php): failed to open stream: No such file or directory in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 10
Warning: include(): Failed opening '../../models/OM.php' for inclusion (include_path='.:/usr/share/php') in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 10
Fatal error: Uncaught Error: Class 'asc\OM' not found in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php:37 Stack trace: #0 /home/alekrabbe/PhpstormProjects/stm_asc/views/cadastro-militar.php(13): asc\MySQL_DataMapper->fetchAllOMs() #1 {main} thrown in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 37
I don't understand why it fails as I did just that on another php file and it worked there. Thank you.
EDIT 1
Forgot to mention but the class OM.php is also in the asc namespace.
Here it is:
<?php
namespace asc{
class OM
{
private $id;
private $nome;
private $sigla;
private $forca_armada;
public function __construct($id, $nome, $sigla, $forca_armada)
{
$this->id = $id;
$this->nome = $nome;
$this->sigla = $sigla;
$this->forca_armada = $forca_armada;
}
[Gets and Sets]
}
}
You're inside a namespace:
namespace asc {
So references to OM become asc\OM. Fix this by anchoring to the root namespace:
$OM = new \OM(...);
Try include __DIR__ . "/../../models/OM.php"; and make sure the path is correct
EDIT: the better way is to use an autoloader as #Alex Howansky mentioned in a comment on his answer, but in a pinch just make sure the path is correct and use DIR.
For some reason it's not detecting the class files in the specified directory.
I'm unsure what the problem is here. Could anyone brighten this up for me?
Code:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class main
{
public static
$test;
}
foreach(glob($_SERVER['DOCUMENT_ROOT'].'/engine/*.php') as $file) {
include($file);
}
main::$test = new Test();
?>
As you can see i'm trying to include all php files from /engine.php/*
Project file structure:
http://prntscr.com/hutqyo
But I'm getting the error
Fatal error: Uncaught Error: Class 'Test' not found in C:\xampp\htdocs\includes\loader.php:23 Stack trace: #0 {main} thrown in C:\xampp\htdocs\includes\loader.php on line 23
Here is my test.class.php code there isn't a lot in it yet. As i'm still working on this current problem:
<title>test</title>
<?php
class Test
{
function getIP(){
}
}
?>
Thanks to anyone that could possibly help me find the solution!
I wrote an autoloader class.
But the include_once function does not include the file I need.
Cls.php(path:cls/)
<?php
class fileup{
function dup($a){
$x=strip_tags($a);
$d=__DIR__."/".$x.".cls.php";
echo $d; ///storage/emulated/legacy/htdocs/yeni/sinif/ayar.sinif.php
if(file_exists($d)){
include_once $d;
}else{
die($a." Class not found :(");
}
}
}
$dup=new fileup();
spl_autoload_register(array($dup,"dup"));
?>
Theme.cls.php Theme class (path:cls/)
<?php
include_once("cls.php");
$dup->dup("conf");
?>
Conf.cls.php Site config class (path:cls/)
<?php include_once("cls.php"); $dup->dup("db"); ?>
index.php (path:/)
<?php
include_once("theme.cls.php");
?>
Error details:
Fatal error: Uncaught Error:
Call to a member function dup() on null in /storage/emulated/legacy/htdocs/yeni/sinif/conf.cls.php:3
Stack trace:
#0 /storage/emulated/legacy/htdocs/yeni/sinif/cls.php(12):
**include_once() #1**
/storage/emulated/legacy/htdocs/yeni/sinif/theme.cls.php(3):
dup->dup('conf') #2
/storage/emulated/legacy/htdocs/yeni/index.php(2):
include_once('/storage/emulat...') #3
{main} thrown inĀ /storage/emulated/legacy/htdocs/yeni/sinif/conf.cls.php on lineĀ 3
I am trying autoload function in php. The files are all in the same directory.
I have a file called aviation.php with the following code:
class Avaitor{
public function __construct(){
echo "Fly high<br>";
}
}
Then in my autoloader file I am trying to do this:
function __autoload($fileName){
if(file_exists($fileName . ".php"))
require_once $fileName . ".php";
}
//require_once "aviatior.php";
$pilot = new Avaitor();
But I am getting this error:
Fatal error: Uncaught Error: Class 'Avaitor' not found in
/Applications/MAMP/htdocs/php_oop/autoload.php:22 Stack trace: #0
{main} thrown in /Applications/MAMP/htdocs/php_oop/autoload.php on
line 22
Just to test that require_once does find my aviator.php I tried it out and then commented it out.
What am I doing wrong here?
I have the following PHP code (below) but cannot seem to get it to run? I think I'm missing something simple like a function (?) or other type of code(s)? It should print: My Antonia: Willa Cather (5.99).
Error message:
Fatal error: Call to undefined method shopProduct::getProducer() in C:\Program Files (x86)
<?php
class shopProduct
{
//class body
}
class ShopProductWriter
{
public function write($shopProduct)
{
$str = "{$shopProduct ->title}: " . $shopProduct->getProducer() . " ({$shopProduct -> price})\n";
print $str;
}
}
// we can test it like this
$product1 = new ShopProduct("My Antonia", "Willa", "Cather", 5.99);
$writer = new ShopProductWriter();
$writer->write($product1);
print "Author: {$product1->getProducer()}\n";
?>