I have a autoload function that looks like so:
function __autoload_HTTP_Client($class_name) {
$HC = $class_name . '.class.php';
return require_once($HC);
}
when i call a class i use the $users = new users();
What i want to do is place my class files in folders within my class directory. So i will have
/users/users.class.php
/general/general.class.php
In my users class i want to be able to extend general.class.php
class users extends general {
}
because I want to be able to use $this-> from general so i dont want to use include. but when i do extend general it says that the class is not found since its in a different directory.
Any ideas on fixing this (I know the autoload needs to change to use directory/ $file.class.php
but how can i extend a class that is outside of the directory of the current class.
Use:
set_include_path(get_include_path().':/users:/general');
The above will then look for files within those directories, and you don't have to use a path, just filename, etc on the include/require inside the autoloader.
Related
I'm trying to trick PHP into taking a class from another namespace when trying to create a specific class.
I have two class called "page", the first is in the Core namespace:
namespace Core;
class Page {...}
The second inherits from Core\Page, but adds a few things. It is in the Addons namespace.
namespace Addons;
class Page extends \Core\Page{...}
The reason I want to do this is because I want to build my system with an easy addon engine. Whenever I want, I can add a line in an XML file that tells the autoloading function to take the class in the addon namespace instead of the core namespace.
However, when I try to do this :
spl_autoload_register('loadClass');
public function loadClass(string $className)
{
if (Addon_exist_and_is_registered($className))
{
require "/Addons/$className.php";
}
else
{
require "/Core/$className.php";
}
}
$page = new \Core\Page(); <-- error here
I get an error saying that the class \Core\Page cannot be found in the file Addons\Page.php. This is normal behaviour since the class is not in the same namespace and as such, the fully qualified name cannot find the right class.
Is it possible to trick PHP into thinking that a child class in another namespace is actually the right class? I tried this for the addons class;
namespace Core;
class Page extends \Core\Page{...}
But it breaks the inheritance as you cannot inherit yourself.
Ignore that the classes have the "same name". Because they don't. One class is called Core\Page, the other is called Addons\Page. Those are their names, their fully qualified names to be exact. It's as much a difference as Foo and Bar. If you tell PHP to instantiate Core\Page, then it's going to do that; you can't "trick" it into instantiating Addons\Page, since that's an entirely different class name.
Don't try to "trick" anyone, make your system actually extensible and explicitly allow overriding of class names:
$class = 'Core\Page';
if (...) {
$class = 'Addons\Page';
}
$page = new $class;
I have created a custom class with few method in it. e.g.
class MyClass{
method1 ();
method2();
}
Now I want to create object of this class and use it inside a controller method
class DefaultController{
public function myAction()
{
//here I want to able to create object of MyClass, is it possible?
}
}
Q1. where should I store this class in symfony2 structuere e.g. inside src dir?
Q2. How can I use this class method inside the controller of a bundle?
If you put your class in the src folder, it will be autoloaded, ie: you can simply do:
$foo = new \MyClass();
$foo->method1();
in your Controller.
A good approach would be to put your classes in the Bundle you are likely to use them:
src/YourCompany/YourBundle/MyClass.php
In this way however don't forget to put the namespace declaration on top of your MyClass file:
namespace YourCompany\YourBundle;
class MyClass{
//..
}
You can put your classes on the base folder of your bundle, or use other nested folders to better differentiate a set of classes from each others, for eg:
src/YourCompany/YourBundle/Listener/MyClassListener.php
src/YourCompany/YourBundle/Manager/MyClassManager.php
For more info see the Best practice on Bundles structure of Symfony2
I'm new to Laravel and using PHP namespaces in general. I didn't run into any problems until I decided to make a model named File. How would I go about namespacing correctly so I can use my File model class?
The files are app/controllers/FilesController.php and app/models/File.php. I am trying to make a new File in FilesController.php.
Namespacing is pretty easy once you get that hang of it.
Take the following example:
app/models/File.php
namespace App\Models;
class File {
public function someMethodThatGetsFiles()
{
}
}
app/controllers/FileController.php
namespace App\Controllers;
use App\Models\File;
class FileController {
public function someMethod()
{
$file = new File();
}
}
Declare the Namespace:
namespace App\Controllers;
Remember, once you've put a class in a Namespace to access any of PHP's built in classes you need to call them from the Root Namespace. e.g: $stdClass = new stdClass(); will become $stdClass = new \stdClass(); (see the \)
"Import" other Namespaces:
use App\Models\File;
This Allows you to then use the File class without the Namespace prefix.
Alternatively you can just call:
$file = new App\Models\File();
But it's best practice to put it at the top in a use statement as you can then see all the file's dependencies without having to scan the code.
Once that's done you need to them run composer dump-autoload to update Composer's autoload function to take into account your newly added Classes.
Remember, if you want to access the FileController via a URL then you'll need to define a route and specify the full namespace like so:
Route::get('file', 'App\\Controllers\\FileController#someMethod');
Which will direct all GET /file requests to the controller's someMethod()
Take a look at the PHP documentation on Namespaces and Nettut's is always a good resource with this article
first, load your class with:
$ composer dump-autoload
then
$file = new File;
// your stuff like:
$file->name = 'thename';
$file->active = true;
$file->save();
Section: Insert, Update, Delete on Laravel 4 Eloquent's doc
To namespace your model, at the top of your model class right after the opening
Then when you call from controllers you will call new Whatever\Model;
You probably have to do a dump-autoload with composer the first time around.
have a look to it.. hopefully will clear your query....
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;
class UserController extends Controller{
public function actionIndex()
{
echo "working on .....";
}
}
Namespaces are defined at the top of PHP classes right after the opening php script tag like this:
<?php
namespace MyNameSpace;
When you then want to use the namespaced class in some other class, you define it like this:
new MyNameSpace\PhpClass;
or import it at the top of the file (after namespaces if present) like this:
<?php
//namespace
use MyNameSpace\MyPHPClass;
//then later on the code you can instantiate the class normally
$myphpclass = new MyPHPClass();
In Laravel namespaces can be defined anywhere composer can autoload them, I'd recommend defining namespaces within the app directory. So you can define a namespace like Utils for holding Utility classes by creating a Utils directory in the app directory, creating our utility classes and defining the namespace as we did above.
Afterwards you have run the command to ask composer to autoload classes:
$ composer dump-autoload
I have a general class which i like to have it extended by other classes.
I have my directory set up with folders and class files inside of those folders for example
Classes/users/users.class.php
classes/general/general.class.php
I have the users class extending the general class but since they are in different folders I guess the general class is not found.
class users extends general {
}
Can someone please help me out figuring this out.
I should also mention i am using autload function
When you have no autoloader then include the class before.
Then the class is known and you can use it.
require_once(__DIR__.'/../general/general.class.php');
You need to make sure you load both classes or any other class that are required. For example:
In your bootstrap...:
// Set up the include path so that we can easily access the class files
set_include_path('/absolute/path/to/classes'. PATH_SEPARATOR . get_include_path());
require_once('users/users.class.php');
In users.class.php:
require_once('general/general.class.php');
class User {
// you class definition
}
As far as getting the absolute path to your classes folder, youll want to configure this based on your bootstrap location. For example:
// bootstrap lives in $_SERVER['DOCUMENT_ROOT'] and classes is one level up outside the doc root
// this code is in the bootstrap...
$path = realpath(dirname(__FILE__).'/../classes');
set_include_path($path . PATH_SEPARATOR . get_include_path());
I am using a custom scheme in naming my files. Any pointers on how to autoload them with Zend's autoloader?
Thanks!
You can write your own autoloader class and register it with the ZF autoloader. There is a brief bit about this in the manual: http://framework.zend.com/manual/en/zend.loader.autoloader.html#zend.loader.autoloader.interface
Basically it's just a class defining one method which takes the class name as the parameter. So for you it would be something like:
class My_Autoloader implements Zend_Loader_Autoloader_Interface
{
public function autoload($class)
{
$filename = $class.'.obj.php';
require $filename;
}
}
the above assumes the classes are on the include path, if not, just stick the full path in front of $filename. Add any other mapping/checking you need in here and then register it as per the example in the manual. Normally you would do this in your bootstrap.