Model View Controller inheritance - php

I have these folder structure:
The Application.php looks so:
<?php
namespace Mensa;
class Application extends \Application\Application{
public function getLayoutViewScript()
{
return __DIR__.'/../../view/index.php';
}
}
My problem is that I want to extend a class in my Application Class in the Mensa Namespace. The class I want to extend is located in Application/Applivation.php (has the same name).
But when I run I get the errot "Fatal error: Class 'Application\Application' not found..."
So how can I inherit the class in Application folder?
Thx!

Related

How to instantiate a Class beside Controller, Model and View in CakePHP 3

I would like to load a Class that I have located in src/Bandit/EpsilonGreedy. EpsilonGreedy itself extends an abstract class located in the same Folder src/Bandit/ABandit.
When I try to load the class with new App\Bandit\EpsilonGreedy(); I nor get an error message nor an instance of the class.
The namespace on top of both PHP-Files i have choosen is namespace App\Bandit;.
Do I have to bypass the autoloader of CakePHP to instantiate such classes in own Folders beside Controller, Model and View?
Thanks!

php 2 classes of the same namespace showing class not found

I have this Project Structure:
BackTest
|MVC
||Controller.php
||View.php
||Model.php
Now when I create the 3 classes
Controller.php:
namespace MVC;
class Controller{}
$mdl=new Model();
View.php:
namespace MVC;
class View{}
Model.php:
namespace MVC;
class Model{}
In the Controller.php file when I am trying to instantiate a Model class I am receiving the error Class 'MVC\Model' not found in even though both of them are in the same name space what could the reason be?
maybe I need an autoload function ?

How to use namespace in a parent class constructor

I have a class named Service_B which extends a custom service class.
This custom service class requires one single object named Reader in its __construct() in order to instantiate properly.
The parent service is defined as follow
namespace Vendor\Services;
abstract class Service{
function __construct(Vendor\Services\Reader $reader){
}
}
Service_B is defined as follow:
namespace Vendor\Services;
class Service_B extends Service{
function __construct(){
parent::__construct(new \Vendor\Services\Reader());
}
}
Reader do have the following line at the top of the file:
use Vendor\Services;
Class files are organized like this:
Vendor/Services/Service_B.php
Vendor/Services/Reader.php
Question:
When I instantiate Service_B, I get the following error message:
Fatal error: Class 'Vendor\Services\Reader' not found
I don't understand why I get this error since I think I am using the proper namespaces declarations. Thank you
On top of your Reader class place:
//This will declare the Reader class in this namespace
namespace Vendor\Services;
and remove:
//THIS IS A WRONG DIRECTIVE: you're telling PHP to use the Vendor\Services class but it doesn't even exist
use Vendor\Services;
Then modify the Service_B class as follow:
namespace Vendor\Services;
//i think this should extend Service, as it's calling the parent constructor
class Service_B extends Service
{
function __construct(){
parent::__construct( new Reader() );
}
}
This way all yours 3 classes will be in the same namespace, and the Reader class should be found without explicit namespace prefix

Error extending Laravel's abstract TestCase class (error is saying that my extended class must be abstract)

I'm hoping somebody out there can help me. I am using laravel 4 and I'm writing my first unit tests for a while but am running into trouble. I'm trying to extend the TestCase class but I'm getting the following error:
PHP Fatal error: Class registrationTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Illuminate\Foundation\Testing\TestCase::createApplication) in /home/john/www/projects/MyPainChart.com/app/tests/registrationTest.php on line 4
Now if I have this right then the error is referring to the fact that is a method is abstract then the class it's in must also be abstract. As you can see from below the TestCase class it is abstract. I have searched for this error but have drawn a blank.
Trying to follow this cast on Laracasts https://laracasts.com/lessons/tdd-by-example and although you have to be a subscriber to watch the video the file is underneath it and as you can see I am doing nothing different to Jeffrey Way.
My Test:
<?php
class registrationTests extends \Illuminate\Foundation\Testing\TestCase
{
/**
* Make sure the registration page loads
* #test
*/
public function make_sure_the_registration_page_loads_ok()
{
$this->assertTrue(true);
}
}
The beginning of the TestCase class:
<?php namespace Illuminate\Foundation\Testing;
use Illuminate\View\View;
use Illuminate\Auth\UserInterface;
abstract class TestCase extends \PHPUnit_Framework_TestCase {
By the way - the Laravel testing class is not autoloaded by default and so I have tried both the fully qualified class name and use Illuminate\Foundation\Testing and then just extending TestCase. I know it can see it aswhen I don't fully qualify the name it complains that the class cannot be found. I've also tried:
composer dump-autoload
and
composer update
Any help appreciated
According to your error message: Class registrationTest contains 1 abstract method the Base Class contains an abstract method and when a Child Class extends another class with abstract methods then the child class should implement the abstract methods available in Base class. So, registrationTest is child class and \Illuminate\Foundation\Testing\TestCase is the base class and it contains an abstract method:
An abstract method in \Illuminate\Foundation\Testing\TestCase:
abstract public function createApplication();
So, in your child class/registrationTest you must implement this method:
public function createApplication(){
//...
}
But, actually you don't need to directly extend the \Illuminate\Foundation\Testing\TestCase because in app/tests folder there is a class TestCase/TestCase.php and you can extend this class instead:
// In app/tests folder
class registrationTest extends TestCase {
//...
}
The TestCase.php class looks like this:
class TestCase extends Illuminate\Foundation\Testing\TestCase {
public function createApplication()
{
$unitTesting = true;
$testEnvironment = 'testing';
return require __DIR__.'/../../bootstrap/start.php';
}
}
Notice that, TestCase has implemented that abstract method createApplication so you don't need to extend it in your class. You should use TestCase class as base class to create test cases. So, create your tests in app/tests folder and create classes like:
class registrationTest extends TestCase {
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}
}
Read Class Abstraction.
Firstly go in composer.json and add
"scripts" : {"test" : "vendor/bin/phpunit"
}
Then run composer update
Then
The TestCase.php class in path /test/ should look like
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;
abstract class TestCase extends BaseTestCase {
use CreatesApplication;
}
Then your registrationTests class should look like this
<?php
namespace Tests\Feature;
use Tests\TestCase;
class registrationTests extends TestCase {}
Just intake dependancy at the top of your class as follows and you are good to go,
<?php
namespace YOUR_CLASS_PATH;
use Tests\TestCase;
class UserTest extends TestCase{
...//your business logic here
}
I hope this works.

Putting a base model extended class in the application/models folder

I created a base_model extends CI_Model class and put it in application/model. Then, I created another class that extends base_model, but I get the following error:
Cannot find this class
Other resources told me to put the base_model in:
application/core
or:
application/libraries
However, I would like to put it in application/models for convenience. Can I put this class in application/models and still have it work correctly?
Try putting a MY_Model class in your application/core folder instead. First-level inheritance was built into CI by default, i.e.:
{APPATH}/core/MY_Model.php:
<?php
class MY_Model extends CI_Model {
public function __construct()
{
parent::__construct();
}
}
In that same file, if you want alternate parent classes, try:
...
class Base_model extends MY_Model {
public function __construct()
{
parent::__construct();
}
}
...
If you don't put it in MY_Model, YOU ARE RESPONSIBLE for loading the base model first (before an extended class references it), AND you can't have a file in your models/ folder called Base_model.php, for example.

Categories