define class globally in withut require include - php

I want to define a class globally, so if I create a page this , it works
<?php
$cl = new MyOwnClass();
?>
How can I do that ?

You shoulod look into Autoloading Classes: http://www.php.net/manual/en/language.oop5.autoload.php

class classname
{
public function publicfuntioname($testvar) {
$this->testvar = $testvar;
}
}
Now include this file any where
$obj= new classname();
$obj->publicfuntioname("another value");

Related

Included class inside another class is not declared PHP

I have the following __construct of a selfmade class;
public function __construct($ip, $user, $pass, $product) {
$this->_ip = $ip;
$this->_user = $user;
$this->_pass = $pass;
$this->_product = $product;
$this->ssh = new Net_SSH2($this->_ip);
if (!$this->ssh->login($this->_user, $this->_pass)) {
return 'Login Failed';
}
$this->sftp = new Net_SFTP($this->_ip);
if (!$this->sftp->login($this->_user, $this->_pass)) {
return 'Login Failed';
}
}
Now the problem is that it says Net_SSH2 and Net_SFTP is not declared, but I have included those classes at the page, im not sure, but can it be that I have to pass those classes into this class instead of just calling them?
If yes, how do I do that?
A better solution is to use autoload.
function __autoload($class_name) {
require_once $class_name . '.php';
}
Now, when you ask for a new class, it will be auto loaded.
Try this.
Have you included those classes using a require, include or an autoload function?
If you haven't, make sure the files in which those classes are defined are loaded.
Now, we need to check for the namespace.
At the top of the Net_SFTP, is there a namespace [SOMETHING]? If there is, you must refer to the fully qualified class name, or use this class.
An example:
<?php
namespace VendorName\BundleName;
class Net_SFTP {
....
}
?>
Now, in order to use this class, we must do one of the following:
<?php
use VendorName\BundleName\Net_SFTP;
...
$this->ssh = new Net_SFTP(...);
...
?>
Or, directly:
<?php
...
$this->ssh = new VendorName\BundleName\Net_SFTP(...);
...
?>

Using a custom class in a Wordpress theme

I have a PHP class with methods that I would like to use anywhere I choose on my theme.For instance this class:
<?php
class MyClass
{
const constant = 'constant value';
function showConstant() {
echo self::constant . "\n";
}
}
$class = new MyClass();
$class->showConstant();
?>
How would I include such a class in my theme?
You have a couple of ways to go about this; you can write a plugin, which might be a bit overkill, but you can also:
1
In your functions.php-file, just add your functions there, and then you can call them in your theme
function myClassFunction() {
class MyClass {
const constant = 'constant value';
function showConstant() {
echo self::constant . "\n";
}
}
$class = new MyClass();
$class->showConstant();
}
2
Create a new directory in your themes folder, something like /includes. Put your class in there. Then wherever in your theme where you need your class and it's functions, just include it in your template:
<?php
require_once('includes/MyClass.php');
$class = new MyClass();
$class->showConstant();
?>
It all depends on what kind of class it is, what it does and how often you use it. There are a whole lot of ways to do it.

Lazy load class in PHP

I want to lazy load class but with no success
<?php
class Employee{
function __autoload($class){
require_once($class);
}
function display(){
$obj = new employeeModel();
$obj->printSomthing();
}
}
Now when I make this
function display(){
require_once('emplpyeeModel.php');
$obj = new employeeModel();
$obj->printSomthing();
}
It works but I want to lazy load the class.
__autoload is a standalone function not a method of a class. Your code should look like this:
<?php
class Employee{
function display(){
$obj = new employeeModel();
$obj->printSomthing();
}
}
function __autoload($class) {
require_once($class.'.php');
}
function display(){
$obj = new Employee();
$obj->printSomthing();
}
UPDATE
Example taken from the php manual:
<?php
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
Change Employee a bit:
class Employee {
public static function __autoload($class) {
//_once is not needed because this is only called once per class anyway,
//unless it fails.
require $class;
}
/* Other methods Omitted */
}
spl_autoload_register('Employee::__autoload');
First if all it's better to use spl_autoload_register() (check the note in php's manual for autoloading).
Then back to your problem; only if the display() function is in the same directory as the employeeModel this will work. Otherwise, use absolute paths (see also include() and include_path setting

PHP different path names spaces and autoloading not working

I'm trying to implement autoloading in Php5.3 using namespaces but I'm having some issues and don't know why it's not working.
I have a basic directory structure of
/root
--bootstrap.php
--test.php
--/src
----/com
------/a
--------Foo.php
------/b
--------Bar.php
bootstrap.php
<?php
function __autoload($class) {
// convert namespace to full file path
echo $class.'<br>';
$class = str_replace('\\', '/', $class) . '.php';
require_once($class);
}
Foo.php
<?php
namespace src\com\a {
class Foo {
public function write() {
echo "write";
}
}
}
Bar.php
<?php
use \src\com\a\Foo;
namespace src\com\b {
class Bar {
public function write() {
$foo = new Foo();
$foo->write();
}
}
}
test.php
<?php
use \src\com\b\Bar;
require_once("bootstrap.php");
$bar = new Bar();
$bar->write();
So the basic premise is call Bar, which in turn includes Foo and calls the write method
output:
src\com\b\Bar
src\com\b\Foo
But when I try and autoload it thinks Foo is in the namespace of src/com/b because that is the namespace of Bar and therefore it doesn't load.
Any ideas on how to fix this?
It looks like bar.php should be:
<?php
namespace src\com\b;
use \src\com\a\Foo;
class Bar {
public function write() {
$foo = new Foo();
$foo->write();
}
}
?>

Forget late static binding, I need late static __FILE__

I'm looking for the get_called_class() equivalent for __FILE__ ... Maybe something like get_included_file()?
I have a set of classes which would like to know what directory they exist in. Something like this:
<?php
class A {
protected $baseDir;
public function __construct() {
$this->baseDir = dirname(__FILE__);
}
public function getBaseDir() {
return $this->baseDir;
}
}
?>
And in some other file, in some other folder...
<?php
class B extends A {
// ...
}
class C extends B {
// ...
}
$a = new A;
echo $a->getBaseDir();
$b = new B;
echo $b->getBaseDir();
$c = new C;
echo $c->getBaseDir();
// Annnd... all three return the same base directory.
?>
Now, I could do something ghetto, like adding $this->baseDir = dirname(__FILE__) to each and every extending class, but that seems a bit... ghetto. After all, we're talking about PHP 5.3, right? Isn't this supposed to be the future?
Is there another way to get the path to the file where a class was declared?
ReflectionClass::getFileName
Have you tried assigning it as a static member of the class?
<?php
class Blah extends A {
protected static $filename = __FILE__;
}
(Untested, and statics plus class inheritance becomes very fun...)
what if you don't use __FILE__ but a separate variable and set the variable to __FILE__ in each class
class A {
protected static $baseDir;
protected $filename = __FILE__; // put this in every file
public function __construct() {
}
public function getBaseDir() {
return dirname($this->filename) . '<br>'; // use $filename instead of __FILE__
}
}
require('bdir/b.php');
require('cdir/c.php');
class B extends A {
protected $filename = __FILE__; // put this in every file
}
$a = new A;
echo $a->getBaseDir();
$b = new B;
echo $b->getBaseDir();
$c = new C;
echo $c->getBaseDir();
you still have to redeclare the property in each class, but not the method
You could use debug_backtrace(). You probably don't want to, though.
Tested:
protected static $filename = __FILE__;
static::$filename
Doesn't work.
It looks like the constant is registered when the class is loaded which is not "late".
Not sure it was possible before, but what was best for me today is using reflection :
$basePath = new \ReflectionObject($this);
$dir = dirname($basePath->getFileName());

Categories