PHP - Is this good require_once or extends - php

I'm a newbie in php but I'll try to get straight to the point.
I have a class called ConnectionManager
class ConnectionManager
{
function ConnectToDB()
{
//PDO connection code
}
}
and in my other manager InstitutManager I am using require_once($filename) to get access to my ConnectionManager functions
require_once('../manager/ConnectionManager.php');
class InstitutManager
{
protected $connInstance;
function _construct()
{
$this->connInstance = new ConnectionManager;
}
function getInstituts()
{
$conn = $connManager->ConnectToDb();
//retrieve instituts
}
}
The question is : Should I be using extends ConnectionManager in my InstitutManager instead of require_once? Why should I use one more than the other?
Thanks
Edit : Changed code for InstitutManager class
Would this be ok like this? Or should I pass a pass a parameter with my connection already instanciated in function _construct($conn)?

Your include_once reads in a source file, which in this case has a class definition for ConnectionManager in it. Your extends sets up class InstitutManager as inheriting class ConnectionManager, i.e. InstitutManager gets everything in ConnectionManager and can then define its own modifications to that basic structure. There isn't really any relationship at all between the two operations, and your $connManager = new ConnectionManager operations are nonsensical.

require_once 'file'.php' just means that the PHP interpreter will take the contents of a file called file.php and dump it right there in the spot where the include was called. Kind of like what would happen if you would select everything in a Word file, click copy and paste it at the top of another Word file.
In your case you need to include the file, or else it will not know where to find the ConnectionManager class.

Related

500 Unable to handle this request

Inside a method in my Model class, I include another PHP file. The code works until that included PHP file declares a class definition ie class Test123 {}.
The class name is unique. The only possible source of the issue that I could think of would be how I'm including a class within a class. However, I wasn't able to find information about this, so I assume it isn't a problem.
Any ideas?
//Inside of method inside of model class
include "{$_SERVER['DOCUMENT_ROOT']}/models/language.php";
I believe you're right in assuming that you can't declare a class inside a class, which is what you're essentially doing by including it inside a class method.
Could you perhaps do something like this?
class.php:
<?php
include("otherclass.php");
class MyClass {
public function run () {
$otherClass = new OtherClass();
}
}
otherclass.php:
<?php
class OtherClass {
}
index.php:
<?php
include("class.php");
$myClass = new MyClass();
$myClass->run();

PHP MVC Call Model in another Model

Sorry if the question has already been asked, but I have wanted to know if import a model in another model MVC is correct or whether it is best to do the same function in EVERY models?
For now in the Model in my Library folder I add the same method as in my Controller:
class Model
{
protected function __construct()
{
$this->db = new Database;
}
public function model($model)
{
require_once '../app/models/' . $model . '.php';
return new $model();
}
}
And in my model file :
class Exemple_model extends Model
{
function __construct ()
{
parent::__construct();
}
public function exemple_function()
{
$otherModel = $this->model('urlAnotherModelFile');
$otherModel->otherMethod();
}
}
But I am not sure it is correct to do so or if is the best way.
Thank you in advance for your answers.
You are kind of mixing things up I think.
Your controller is responsible for creating models, and executing methods on them. You can rename you Model class to BaseModel, and make each model extend from this BaseModel.
As for the problem of including the right files, I would recommend you take a look at the autoloader: http://php.net/manual/de/function.spl-autoload-register.php. This may look a bit confusing at first, but if look at the first example you will quickly understand what is the gist of it: basically if you want to create a object of a specific class, php checks if it already knows that class, and if not, it will execute the autoloader (which then tries to include the file containing the class). As soon as you have configured the autoloader, you can stop caring about including the right files.
As autoloader might be a bit confusing at first, this would be the autoloader in your specific use case:
spl_autoload_register(function ($model) {
include '../app/models/' . $model. '.php';
});
You need to put this snippet somewhere before you construct / access a model. Everything else is done by php (it calls automatically the function as it needs to, so calling something like spl_autoload_register('Comments_model::comments_list'); would not work at all :)
Now if you want to execute a method from a model, you can do this as following:
$otherModel = new otherModel();
$otherModel->otherMethod();
no worries about require or similar, php does this for you.

Abstraction class recognition

Where do I put includes to get the close to properly register? I have tried including both command base and close in ATE, and I've tried including just command base in ATE, with close in command base... no matter what I do, it says Close not found.
class.CommandBase.php
<?php
abstract class CommandBase {
// variables
// abstract functions
// protected function
}
?>
class.Close.php
<?php
class Close extends CommandBase
{
// variables
// functions
// public function __construct
}
?>
class.ATE.php
<?php
class ATE {
// instance variables
// public properties
public function start(){
$command = new Close(); // class 'Close' not found
}
// public function __construct
}
?>
I have tried including both command base and close in ATE
This should work, provided 'command base' is include before 'close'.
include ('class.CommandBase.php');
include ('class.Close.php');
and I've tried including just command base in ATE, with close in
command base
'close' in 'command base' won't work, since 'close' extends 'command base'.
Ideally, I would avoid including class files in other class files, and confine my include statements to whatever script is using the classes.
Class Close needs to include Class CommandBase. Class ATE needs to include Close
class.Close.php
include 'class.CommandBase.php';
class.ATE.php
include 'class.Close.php';
Be aware that this will work as is IF all the files are in the same directory. If they are NOT in the same directory you will need to update this to point to the right location.
You also should look at PHP's method for autoloading classes. BIG help!
http://php.net/manual/en/language.oop5.autoload.php

PHP Class path confliction when using include

I have a problem with including classes. Here's a simple example to explain the problem:
Class no. 1 (path: /home/day/Class_A.php):
class Class_A {
public function sayHi() {
echo "Good day";
}
}
Class no. 2 (path: /home/test/Class_B.php):
class Class_B {
public function greeting() {
if(class_exists("Class_A")!=true)
include "../day/Class_A.php";
$test = new Class_A();
$test->sayHi();
}
}
PHP file (path: /home/php.php)
if(class_exists("Class_B")!=true)
include "test/Class_B.php";
$g = new Class_B;
$g->greeting();
The problem is when php.php includes Class_B and Class_B includes Class_A, Class_B fails to open Class_B because the path of the object of the class is now the same as the php.php file.
My question is:
Is there a good and simple way to get around this?
Try changing:
include "../day/Class_A.php";
to
include dirname(__FILE__) . '/../day/Class_A.php';
This way, your include will be relative to the file that is doing the include (Class_B).
Looks like greeting is not a static function so you can't do:
Class_B::greeting();
You'd have to get a class B object and called greeting on it or add static to greetings declarations.
Second, why not uuse require_once? In php.php
require_once('test/ClassB.php');
And in ClassB.php:
require_once('../day/ClassA.php');
Use __autoload which is a magic function, that you define, that enables PHP to let you know when it doesn't have a class loaded, but that class needs to be loaded.
If you define the __autoload function like so,
function __autoload ($classname)
{
require('/path/to/my/classes/'.$classname.'.php');
}
you no longer need to add
require_once('/path/to/my/classes/MyClass.php');
into your files, because the first time that PHP encounters
$mine = new MyClass();
or
MyClass::staticMethodCall();
it will automatically call the __autoload function that you defined earlier.
__autoload('MyClass');
Resource: http://blog.samshull.com/2010/02/why-you-should-use-autoload-function-in.html

How can I initiate a PHP class and use it in several files?

I am stumped right now. In my last post about this question the answer was to use a singleton to make sure an object is only initiated 1 time but I am having the opposite problem.
If I have a file called index.php and then I include these files into it, class1.php, class2.php, class3.php, class4.php.
In index.php I will have,
<?PHP
$session = new Session();
require_once '/includes/class1php';
require_once '/includes/class2.php';
require_once '/includes/class3.php';
require_once '/includes/class4.php';
?>
then in all 4 of the test files I will try to access a method called get() from the session class, assume the session class file is already included into the index.php page as well.
Now if I try to use...
$testvar = $session->get($var1);
in any of the test class files I will get this error
Fatal error: Call to a member function get() on a non-object
the only way the code works without an error is if I use
$session = new Session();
in every file.
How can I fix/avoid having to initaite the class in every file when it is already initated in the index.php file?
the goal is to let me initiate a class in 1 file like index.php and then include the class files into that page, the catch is most of the classes use methods from other classes so would be nice if I didn't have to initiate every class in every file
Without seeing the code it's hard to tell, but I think I can make some assumptions. correct me if I'm wrong:
EDIT: So post your source so we can stop speculating
1) The files you are including are class files. in other words, they contain something like:
class a
{
function a(){}
function b()
{
}
}
2) You aren't trying to execute code in the class files, at load time, but at some later time by instantiating them
i.e.
require("class.a.php");
$myA = new a();
$a->b();
If you are trying to reference your session variable inside those classes, then you have a scope issue. A variable declared outside a class definition can't be used inside the class, unless it is declared as a global var inside the class.
class a
{
function a(){}
function willFail()
{
$session->doSomething(); //fails
}
function b()
{
global $session;
$session->doSomething(); //succeeds
}
}
Even then, you probably don't want to do that, but instead you should pass in your session as a variable if the class needs access to it:
class a
{
function a(){}
function b($session)
{
$session->doSomething(); // yay!
}
}
You could have a base class they all all extend from
Example
class test1 extends Base {
public function doSomething() {
$this->session->get('something');
}
}
class Base {
protected session;
public function __construct() {
$this->session = new Session();
}
}
You're kind of thinking about it backwards. Any file that will use the session object will need to include the file containing that class definition. The alternative is to use __autoload to pull the class in:
function __autoload($classname)
{
if ($classname == 'Session')
{
include_once 'Session.php';
}
}
EDIT : you'll need to put the file containing that autoload into every file that will use it.

Categories