Setting up controller classes the correct way - php

I will try to explain myself the best way possible. I am trying to start with OOP in PHP. I started this way, i made a "main controller" with a construct where i initiated all my other controllers. I did this because i was hoping to share data along controllers.
class clsController{
public function __construct($smarty){
if (is_object($smarty)) {
$this->o_smarty = $smarty;
} else {
throw new Exception("Smarty not set!");
}
$this->o_clsSharedAdsController = new clsSharedAdsController($this->o_smarty);
$this->o_clsSharedUserController = new clsSharedUserController($this->o_smarty);
}
}
So far it looks pretty oke in my opinion. Now the thing is when i am in the class clsSharedUserController which looks like this:
class clsSharedUserController extends clsController{
// construct
public function __construct($smarty) {
if (is_object($smarty)) {
$this->o_smarty = $smarty;
} else {
throw new Exception("Smarty not set!");
}
}
}
I cannot access a function in the clsSharedAdsController. The controller looks like this.
class clsSharedAdsController extends clsController{
// construct
public function __construct($smarty) {
if (is_object($smarty)) {
$this->o_smarty = $smarty;
} else {
throw new Exception("Smarty not set!");
}
}
// ad details used for messages
public function getAdDetailsForMessage($ads_id){
echo $ads_id;
}
}
I am trying to do access it the following way
class clsSharedUserController extends clsController{
// construct
public function __construct($smarty) {
if (is_object($smarty)) {
$this->o_smarty = $smarty;
} else {
throw new Exception("Smarty not set!");
}
}
// ad details used for messages
public function getUserReviews($userReviews){
$this->o_clsSharedAdsController->getAdDetailsForMessage(1);
}
}
To be honest i expected to get my 1 echo-ed but i am getting this error:
Fatal error: Uncaught Error: Call to a member function getAdDetailsForMessage() on null in /home/vhosts/gamermarket.nl/httpdocs/includes/controllers/shared/clsSharedUserController.php:124
Stack trace:
#0 /home/vhosts/gamermarket.nl/httpdocs/includes/controllers/shared/clsSharedUserController.php(68): clsSharedUserController->getUserReviews(Array)
#1 /home/vhosts/gamermarket.nl/httpdocs/includes/controllers/clsReviewController.php(17): clsSharedUserController->getUserReviewsFromUsersId('0000000001')
#2 /home/vhosts/gamermarket.nl/httpdocs/reviews.php(10): clsReviewController->getReviews()
#3 {main} thrown in /home/vhosts/gamermarket.nl/httpdocs/includes/controllers/shared/clsSharedUserController.php on line 124
I am using a PDO class for my queries to get data they are set-up the same way as this clsController. I am probally missing some logic for a correct set-up to share my data along all classes without creating new instances all the time. You should be able to just make 1 instance of a class and share it among other classes, not?

If you want to use some clsSharedUserController's method in clsSharedAdsController
and use some clsSharedAdsController's method in clsSharedUserController
you need to redesign the class
In clsController
class clsController{
protected $o_smarty;
public function __construct($smarty){
if (is_object($smarty)) {
$this->o_smarty = $smarty;
} else {
throw new Exception("Smarty not set!");
}
}
public function getSharedUser()
{
return new clsSharedUserController($this->o_smarty);
}
public function getSharedAds()
{
return new clsSharedAdsController($this->o_smarty);
}
}
In clsSharedUserController
class clsSharedUserController extends clsController{
// construct
public function __construct($smarty) {
parent::__construct($smarty);
}
// ad details used for messages
public function getUserReviews($userReviews){
$sharedAds = $this->getSharedAds();
$sharedAds->getAdDetailsForMessage(1);
}
}
In clsSharedAdsController
class clsSharedAdsController extends clsController{
// construct
public function __construct($smarty) {
parent::__construct($smarty);
}
// ad details used for messages
public function getAdDetailsForMessage($ads_id){
echo $ads_id;
}
//use the method in share user class
public function someMethod(){
$sharedUser = $this->getSharedUser();
//TODO
}
}

I think your main class should not new child class inside construct
In clsSharedUserController
class clsController{
public function __construct($smarty){
if (is_object($smarty)) {
$this->o_smarty = $smarty;
} else {
throw new Exception("Smarty not set!");
}
}
}
In clsSharedUserController
class clsSharedUserController extends clsController{
protected $o_clsSharedAdsController;
// construct
public function __construct($smarty) {
parent::__construct($smarty);
$this->o_clsSharedAdsController = new clsSharedAdsController($smarty);
}
// ad details used for messages
public function getUserReviews($userReviews){
$this->o_clsSharedAdsController->getAdDetailsForMessage(1);
}
}
In clsSharedAdsController
class clsSharedAdsController extends clsController{
// construct
public function __construct($smarty) {
parent::__construct($smarty);
}
// ad details used for messages
public function getAdDetailsForMessage($ads_id){
echo $ads_id;
}
}

You have to create an instance of the clsSharedAdsController class before you can use its methods.
o_clsSharedAdsController = new clsSharedAdsController($smarty);
o_clsSharedAdsController->getAdDetailsForMessage(1);

Related

Write a class in PHP Symfony 4.4 using the design pattern Factory Method to instantiate Service classes

Using Symfony 4.4 with autowiring activated, I want to instantiate a class using the design-pattern FactoryMethod.
The class instantiated is a service with autowired arguments passed into the constructor.
It work well if the constructor is the same for each type of class to instantiate inside the factory method.
But, each service to instantiate has to autowire some specific service in order to work.
I found that we could use the "setter dependency injection". Articles describing it:
https://symfonycasts.com/screencast/symfony-fundamentals/logger-trait
https://symfony.com/doc/4.4/service_container/injection_types.html#setter-injection
I tried to implement the setter dependency injection but the code inside is never executed.
Considering the articles, we should enter the setters with the PHPDoc "#required" immediately after the __construct method has been called (from what I understood).
It doesn't work with my code (see below).
Is my implementation correct?
Is there a better way of doing it?
My code looks like:
// Controller
/**
*#Route("/my_action/{param}")
*/
public function my_action (ThingManagerFactory $thingManagerFactory, $param)
{
$thingManager = $thingManagerFactory->get($param);
$thingManager->doSomething();
}
// ThingManagerFactory
class ThingManagerFactory
{
private $firstManager;
private $secondManager;
private $thirdManager;
public function __construct(FirstManager $firstManager, SecondManager $secondManager, ThirdManager $thirdManager)
{
$this->firstManager = $firstManager;
$this->secondManager = $secondManager;
$this->thirdManager = $thirdManager;
}
public function get($param): ThingManagerInterface
{
if($param == 1) {
return new Thing1Manager(
$this->firstManager,
$this->secondManager,
$this->thirdManager,
);
} elseif($param == 2) {
return new Thing2Manager(
$this->firstManager,
$this->secondManager,
$this->thirdManager,
);
}
throw new \InvalidArgumentException("...");
}
}
// ThingManagerInterface
interface ThingManagerInterface
{
public function __construct(
$this->firstManager,
$this->secondManager,
$this->thirdManager,
);
public function doSomething();
}
// Thing1Manager
class Thing1Manager implements ThingManagerInterface
{
(...)
private $spec1Manager;
public function __construct(
$this->firstManager,
$this->secondManager,
$this->thirdManager,
)
{
(...)
}
/**
* #required
*/
public function setSpecificManager(Spec1Manager $spec1Manager)
{
// this code is never called
$this->spec1Manager = $spec1Manager;
}
public function doSomething()
{
// we pass here before going into setSpecificManager
(...)
}
}
// Thing2Manager class
// is similar to Thing1Manager with multiple other specific managers.
Thank you for your help.
In order to use the design-pattern Factory Method with Symfony, use the Service Locator to provide autowire outside a Controller.
Refactor the code to the following:
// Controller
/**
*#Route("/my_action/{param}")
*/
public function my_action (ThingManagerFactory $thingManagerFactory, $param)
{
$thingManager = $thingManagerFactory->get($param);
$thingManager->doSomething();
}
// ThingManagerFactory
use App\Locator\ThingLocator;
class ThingManagerFactory
{
private $locator;
public function __construct(ThingLocator $locator)
{
$this->locator = $locator;
}
public function get($param): ThingManagerInterface
{
if($param == 1) {
return $this->locator->get(Thing1Manager::class);
} elseif($param == 2) {
return $this->locator->get(Thing2Manager::class);
}
throw new \InvalidArgumentException("...");
}
}
// ServiceLocatorInterface
interface ServiceLocatorInterface
{
public function get(string $id);
}
// ThingLocator
class ThingLocator implements ServiceLocatorInterface, ServiceSubscriberInterface
{
private $locator;
public function __ construct(ContainerInterface $locator)
{
$this->locator = $locator;
}
public function get(string $id)
{
if (!$this->locator->has($id)) {
throw new \Exception("The entry for the given '$id' identifier was not found.");
}
try {
return $this->locator->get($id);
} catch (ContainerExceptionInterface $e) {
throw new \Exception("Failed to fetch the entry for the given '$id' identifier.");
}
}
public static function getSubscribedServices()
{
return [
Thing1Manager::class,
Thing2Manager::class,
];
}
}
// ThingManagerInterface
interface ThingManagerInterface
{
public function doSomething();
}
// Thing1Manager
class Thing1Manager implements ThingManagerInterface
{
// ...
private $spec1Manager;
public function __construct($firstManager, $secondManager, $thirdManager, $spec1Manager)
{
// ...
}
// This setter is no more needed. This manager can be added to the constructor method.
// **
// * #required
// */
//public function setSpecificManager(Spec1Manager $spec1Manager)
//{
// if not commented, this code would be called thanks to the Service Locator (which is a Symfony Service Container)
// $this->spec1Manager = $spec1Manager;
//}
public function doSomething()
{
// ...
}
}

Php remove () in a function to make it easier to use

I am creating a little system which will allow users to extend the system with their own classes.
Class Core {
static $confArray;
static $extendArray;
protected static $instance;
public function read($name)
{
return self::$confArray[$name];
}
public function put($name, $value)
{
self::$confArray[$name] = $value;
}
public function extend($function, $handler, $args=null){
self::$extendArray[$function] = new $handler($args);
}
public function __call($method, $args){
return self::$extendArray[$method];
}
public static function getInstance()
{
if (!isset(self::$instance))
{
$object =__CLASS__;
self::$instance= new $object;
}
return self::$instance;
}
}
With That, now a user can come and register a simple extension from such a class:
class WorkersTest{
function isWorking($who){
echo "$who is working";
}
function isNotWorking($who){
echo "$who is not working";
}
}
To call the function (isworking/ isNotWorking), a the programmer needs to register the test class through:
Core::getInstance->extend("worker","WorkersTest");
Then it can now be called through:
Core::getInstance->worker()->isWorking("George");
This is working perfectly. My question is how i can remove the () in the call (dont worry why) and have:
Core::getInstance->worker->isWorking("George");
Is it possible?
You can use the magic __get() method, just like __call():
public function __get($name)
{
return $this->$name();
}
Try overriding the magic __get() method to return what you need:
Class Core {
// (...)
public function __get($name) {
if (isset( self::$extendArray[$function] )) {
return $this->$name();
}
//if there is no function registered under named "$name"
//throwing Exception is by design better, as #scragar suggested
throw new Exception("No function registered under named {$name}");
//return NULL;
}
}

PHP Method Chaining: Calling one method before allowing other methods to be chained

Consider the following class
class myClass {
private $model;
public function update($input) {
return $this->model->update($input);
}
public function find($id) {
$this->model = ORMfind($id);
}
}
How do I prevent
$myClass = new myClass;
$myClass->update($input);
The problem isn't HOW to use the above code but how to make update() a method only callable after find().
EDIT: I changed what my method does so it was more clearly understood that I need to do one method (find()) before another (update())
You could add a flag to your code like so:
class myClass {
private $model;
private $canUpdate = 0;
public function update($input) {
if ($canUpdate === 0) return; // or throw an exception here
return $this->model->update($input);
}
public function find($id) {
$this->model = ORMfind($id);
$canUpdate = 1;
}
}
Setting the flag $canUpdate will caution the update() method to react accordingly. If update() is called, you can throw an exception or exit out of the method if the flag is still 0.
To prevent from returning null value by get :
public function get() {
if (isset($this->value)) return $this->value;
else echo "please give me a value ";
}
You can also create a construct:
function __construct($val){
$this->value=$val;
}
and then give a value to your $value without using set() method:
$myClass=new myClass(10);
Outputting text, returning void, I think all of this is wrong. When you do not expect something to happen, you should throw an exception:
class MyClass {
private $canUpdate = false;
public function find($id) {
// some code...
$this->canUpdate = true;
}
public function canUpdate() {
return $this->canUpdate;
}
private function testCanUpdate() {
if (!$this->canUpdate()) {
throw new Exception('You cannot update');
}
}
public function update($inpjut) {
$this->testCanUpdate();
// ... some code
}
}
Now you can do:
$obj = new MyClass();
try {
$obj->update($input);
} catch (Exception $e) {
$obj->find($id);
$obj->update($input);
}
The proper way to make sure ->update() can only be called when the model has been initialized is to turn it into a dependency:
class myClass
{
private $model;
public function __construct($id)
{
$this->model = ORMfind($id);
}
public function update($input) {
return $this->model->update($input);
}
}
$x = new myClass('123');
Alternatively, if you have multiple find operations, you could introduce them as static constructor methods:
class myClass
{
private $model;
private function __construct($model)
{
$this->model = $model;
}
public function update($input) {
return $this->model->update($input);
}
public static function find($id)
{
return new self(ORMfind($id));
}
}
$x = myClass::find('123');
Update
Tackling your immediate problem can be done by a simple check:
public function update($input) {
return $this->model ? $this->model->update($input) : null;
}

PHP : Function argument must be an Object with dynamic class name

so I am new in the world of object oriented programming and I am currently facing this problem (everything is described in the code):
<?php
class MyClass {
// Nothing important here
}
class MyAnotherClass {
protected $className;
public function __construct($className){
$this->className = $className;
}
public function problematicFunction({$this->className} $object){
// So, here I obligatorily want an $object of
// dynamic type/class "$this->className"
// but it don't works like this...
}
}
$object = new MyClass;
$another_object = new MyAnotherClass('MyClass');
$another_object->problematicFunction($object);
?>
Can anyone help me ?
Thanks, Maxime (from France : sorry for my english)
What you need is
public function problematicFunction($object) {
if ($object instanceof $this->className) {
// Do your stuff
} else {
throw new InvalidArgumentException("YOur error Message");
}
}
Try like this
class MyClass {
// Nothing important here
public function test(){
echo 'Test MyClass';
}
}
class MyAnotherClass {
protected $className;
public function __construct($className){
$this->className = $className;
}
public function problematicFunction($object){
if($object instanceof $this->className)
{
$object->test();
}
}
}
$object = new MyClass;
$another_object = new MyAnotherClass('MyClass');
$another_object->problematicFunction($object);
That's called type hinting and what you want to do is just not supported.
If all those dynamic class names have something in common (e.g., they're different implementations for certain feature) you probably want to define a base (maybe abstract) class or an interface and use that common ancestor as type hint:
<?php
interface iDatabase{
public function __contruct($url, $username, $password);
public function execute($sql, $params);
public function close();
}
class MyClass implements iDatabase{
public function __contruct($url, $username, $password){
}
public function execute($sql, $params){
}
public function close(){
}
}
class MyAnotherClass {
protected $className;
public function __construct($className){
$this->className = $className;
}
public function problematicFunction(iDatabase $object){
}
}
Otherwise, just move the check to within problematicFunction() body, as other answers explain.

chain of resposibility code error for PHP

I have the following PHP code as chain of resposibility, I am using PHP5.4.9.
abstract class Logger
{
protected $next;
public function next($next)
{
$this->next = $next;
return $this->next;
}
public function run(){
$this->invoke();
if(null!=$this->next){
$this->next->invoke();
}
}
abstract public function invoke();
}
class EmailLogger extends Logger
{
public function invoke()
{
print_r("email\n");
}
}
class DatabaseLogger extends Logger
{
public function invoke()
{
print_r("database\n");
}
}
class FileLogger extends Logger
{
public function invoke()
{
print_r("file \n");
}
}
$logger = new EmailLogger();
$logger->next(new DatabaseLogger())->next(new FileLogger());
$logger->run();
the expect output is:
email
database
file
but the actually output:
email
database
I hope to implement chain of resposibility design pattern by PHP language, one abstract class and three or more classes to do something as a chain. but only the first two object works.
Anyting missing? Or PHP can not use this coding style under PHP5.4.9?
Thanks.
Replace
public function run() {
$this->invoke ();
if (null != $this->next) {
$this->next->invoke();
}
}
With
public function run() {
$this->invoke ();
if (null != $this->next) {
$this->next->run ();
}
}
please try $this->next->invoke() change $this->next->run()

Categories