How to include php page has my class - php

I have a problem while including my own php class page from its server.
I hosted my php classes page like http://my.website.com/phpclasses.php
and tried to call it from another host such as:
<?php
include ("http://my.website.com/phpclasses.php");
?>
By the way, the php class page has a class named 'test'
and activated in it by this variable, here is a quick view about it:
phpclasses.php
class test{
somefunctions();
somefunctions();
}
$test = new test();
I requested this page from another website like this:
anotherwebsite.php:
$newclass = new test();
$newclass->somefunctions();
Notice that i've activated the class twice, once in its page, and again in the another website, but failed and returns nothing.
How could it work?

It's a bad idea to include over HTTP unless your printing PHP Code to the browser. Otherwise it will not work.
It's best to include locally:
Class.php
class Test{
public function __construct(){
return TRUE;
}
}
index.php
include "Class.php";
$Class = new Test();
Both files are located on the same server (same directory).. if including in other directories, add correct path to the file:
include "/Core/Class.php";
Being interpreted as
/ - Root Directory
Core - Sub directory
Class.php - File name

Related

PHP: Understanding the include scope with spl_autoload_register

I'm trying to autoload Classes, but I'm hindered by the scope. The Class is being loaded, but I'm using it out of scope. Here's the code to further explain what I'm trying to do.
AutoLoader.php
<?php
class AutoLoader {
private $namespace;
public function __construct($namespace) {
$this->namespace = $namespace;
spl_autoload_register(array($this, 'ClassLoader'));
}
private function ClassLoader($class) {
$class = "classes/{$this->namespace}/{$class}.php";
print("Loading class: {$class}");
include "{$class}";
}
}
?>
script.php
<?php
ini_set("display_errors", 1);
$loader = new AutoLoader("myspace");
$MyClassObj = new MyClass();
$result = $MyClassObj->MyClassFun();
?>
So when it comes down to script.php, I get the print out that it's loading the class and I don't get any errors that it can't find the file. So it looks like it's loading, but when I use the class to create a new object, it tells me it can't find the class. So I'm loading the include out of scope.
I included the AutoLoader in a separate file so I could load it into multiple files. Am I able to make this work or must the AutoLoader be part of script.php instead of separate?
edit: Including error, added error display to script.php.
Loading class: classes/myspace/MyClass.php
Fatal error: Class 'MyClass' not found in /usr/local/apache2/htdocs/scripts/script.php on line 5
edit: Including MyClass.php and directory structure
MyClass.php
<?php
class MyClass {
public function MyClassFun() {
$var = "hello world";
return $var;
}
}
?>
Directory Structure
htdocs/scripts/script.php
htdocs/classes/myspace/MyClass.php
htdocs/AutoLoader.php
If I change the class to MyClasse (misspelled) it will error on the include because it can't find the file. So using the proper MyClass I can only assume that it's finding the file since it's not producing an error. So the include looks good, but it still won't use it.
Which, now that I think about it more, is strange. The include is only occuring because of line 5 when I go to use the class. ClassLoader is being called by the spl_auto_register to search for the class. It's finding and including the class. Yet the same line 5 fails to actually use it.
I guess I don't understand that disconnect. Line 5 is properly calling the ClassLoader, but then fails to actually find the class once loaded.

How do I access resource files in a php class

I have a class named CMS which relies on a file names "DB.json"
I am using the following code:
class CMS{
function __construct(){
$DB = json_decode(file_get_contents("DB.json"));
}
}
which works fine as long as the file I am requiring the class from is inside the same directory
so if "/classes/cms/lib.php" is my class file and require "lib.php" inside the file "/classes/cms/form.php" then it will work
but if I require "/classes/cms/lib.php" from inside of a file such as "/home.php" then I get an error message saying that the file "DB.json" doesn't exist
link: http://www.streamlinedesign.ml/testcms.php
Use magic constant __DIR__ - it will indicate the directory of the current file (from which the constant is accessed):
class CMS{
function __construct(){
$DB = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR."DB.json"));
}
}

PHP can not create instance of class with namespace

I've got the following problem:
This is my super basic class:
class A
{
function foo()
{
echo "bar";
}
}
Now in front of the class declaration I use the following code:
$a = new A();
$a->foo();
When I open the php file in the browser, the output is "bar". Fine!
Now I want to do the same thing in another file.
directly in the first place I declare the following namespace:
namespace model\dbAction;
This is the path where my file with the class above is located.
So in another php file I do the following:
$a = new \model\dbAction\A();
$a->foo();
But I don't get any output and other code after that won't run so it looks like it breaks directly after the instancing of the class.
Any ideas why instancing the class in another file is not working?
Thanks!
Full code first php file:
<?php
namespace model\dbAction;
class A
{
function foo()
{
echo "bar";
}
}
Full code of the second file (which I call in the browser):
$a = new \model\dbAction\A();
$a->foo();
You still need to include the file -- providing the namespace itself will not include the file for you... unless you're using an autoloader. See: How do I use PHP namespaces with autoload?

CakePHP How to call a controller function from an external function

I'm having trouble with accessing the session in an external .php script located in webroot.
Thought I'd write a function getSession() in one of my controllers and try to call it in the .php file.
So in steps:
I have file.php
In a controller I have a function getSession().
How to call the controllers function in the file.php?
Thank you.
EDIT
Meanwhile I fixed my bug, but still am curious how this is done and want other stack users to find a good answer to this so:
Its exactly like this:
In UsersController I have a function:
public function getSession() {
return $_SESSION['Auth']['User']['user_id'];
}
That I want to let's say print (for example) like this: print_r(Users.getSession) in the file test.php located in webroot/uploadify/test.php.
This file is not a class, but if it is required, then it shall be :)
#CaboOne: Maybe your answer was correct, I just wasnt sure what code to call (and enter) where :)
Supposed I have the following php file in webroot folder:
<?php
class TestingClass {
function getName(){
return "Test";
}
}
?>
I would do the following:
// This would bring you to your /webroot folder
include $_SERVER['DOCUMENT_ROOT'].'/another_file.php';
// Initializing the class
$example = new TestingClass;
// Call a function from the initialized class
$a_value = $example->getName();
// If you want to use $a_value in the view, you can then set
$this->set('a_value', $a_value);

Can I have a class method include a header file for me in PHP?

Please review the example code below, I have a class file that is loaded into a config file. The config file is then loaded into any page I build. Is it possible to include a header file the way I have in the show_header() method? It doesn't seem to work so how can I achieve this result?
// Core.class.php
class Core
{
public function show_header($page_name){
require_once 'includes/header.inc.php';
}
}
// config.inc.php
require_once 'Core.class.php';
$core = New core;
// testpage.php
require_once 'config.inc.php';
$core->show_header('home');
Here is the top part of the header.inc.php file I am trying to include into the page, it seems to work including it but it breaks the way the header file works.
//header.inc.php
<?PHP
//start page timer
$session->get('user_id');
$profiler = new Profiler;
$profiler->start();
//see if site is turned on/off
$core->sitestatus($config['site_status']);
This part gives me errors like this...
Notice: Undefined variable: session in
C:\webserver\htdocs\friendproject2\includes\header.inc.php
on line 5
Fatal error: Call to a member function
get() on a non-object in
C:\webserver\htdocs\friendproject2\includes\header.inc.php
on line 5
When you're including a file from within a function it's just as if you wrote the code within that file from within that function.
e.g.
file foo.php:
<?php
echo $foo->getFoo();
file bar.php
<?php
class Foo {
public function getFoo() {return 'foo';}
}
$foo = new Foo();
function bar()
{
require 'foo.php';
}
bar();
The above will result in the following notice/error being thrown, because $foo is not known within bar().
Fatal error: Call to a member function getFoo() on a non-object in /Users/hobodave/foo.php on line 3
Edit:
I'm not sure what your "Core" class fully entails, but you could perhaps use it as a type of storage for your "globals".
e.g.
<?php
$session = new Session();
$core->session = $session;
Then your $session would be accessible in your header using $this->session
re your comment, sounds like you need a root web context object that you reference the other objects from:
$ctx = WebContext::get();
$ctx->session->get('x');
$ctx->input->get('y');
$ctx->identity->valid;
etc... this is how most web frameworks do it.
$session would need to be defined, then referenced in the included file:
// If a global variable:
global $session;
$session->get('x');
// If a member of Core:
$this->session->get('x');
yes you can do that, probably you'll want require instead of require_once, and the paths would need to be based on the current working directory or an absolute path
try adding error_reporting(E_ALL) to see if any notices are happening...
All calls you make inside the header file will be called as if they were local calls inside the show_header function. So if you want to use any global variable, you will have to use global $variablename; on the top of the included file (or in the beginning of the show_header function).
If you use a static function for the session class you wouldn't need to define it in the same file. http://php.net/manual/en/language.oop5.static.php
You are trying to access $session which is out of scope as pointed in another answer.
Since session stuff is usually global throughout most apps consider using the singleton pattern for the Session class.
This way you can do something like $session = Session::getInstance().
This lets you use the session class anywhere and you usually only one need one instance of a session class (usually). Take a look at Zend Framework for examples on singleton classes.

Categories