I want to overwrite a static method with a non static method. I'm getting this error:
Fatal error: Cannot make static method Person::say() non static in class Coder on line 22
I want to overwrite a classes parent static method makeCall() with a more specific makeCall() that would be non-static.
Code:
<?php
class Request {
public static function makeCall($url) {
// Do some cURL stuff...
}
}
class API extends Request {
const PRODUCTS = '/products';
private $api;
public function __construct($api) {
$this->api = $api;
}
public function getProducts() {
$this->makeCall(self::PRODUCTS);
}
public function makeCall($service) {
parent::makeCall($this->api . $service);
}
}
I could make the parent makeCall() non-static, but I also want to be able to Request:makeCall() in some places.
You may simply change your methods name. And call it with the new name.
There is no option to have it the same name as a static method of the class you're extending.
Related
I am building an MVC component and I'm getting stuck with an issue with a parent and child model. I have a few methods in the parent Model and they're not working with the database_class object
the constructor works fine
but when I use that object in the methods its like the constructor doesn't exist?
Class Controlller
{
public function __construct()
{
$this->childModel = $this->model('childModel');
} // end construct
// methods go here
}
Here are the models:
class childModel extends parentModel {
private $dbo;
public function __construct()
{
$dbobj = new Database_class;
$this->dbo = $dbobj;
}
//methods
}
class parentModel {
private $dbom;
public function __construct()
{
$dbombj = new Database_class;
$this->dbom = $dbombj;
var_dump($this->dbom); //working perfectly as database object
}
public function methodName()
{
var_dump($this->dbom); //not showing up as database object
}
}
I don't think this code is doing what you think it's doing. In childModel, you are overwriting the __construct method of the parentModel, so the __construct in the parentModel never gets called. Therefore $this->dbom should be null. Furthermore if you wish to use $this->dbom from the childModel, you should probably change the scope from private $dbom to protected $dbom. See this page for more info on that: http://php.net/manual/en/language.oop5.visibility.php
I've created a class called Boot, inside this I've a method that change the path of a file, so the user can call it to set a custom path, something like this:
class Boot
{
private static $_filePath = 'directory/';
public function __construct()
{
require 'system.php';
}
public function init()
{
new System();
}
public function setFilePath($newDir)
{
$this->_filePath = $newDir;
}
public static function getFilePath()
{
return self::_filePath;
}
}
so in my index.php file:
require 'boot.php';
$b = new Boot();
$b->setFilePath('directories/');
$b->init();
Now in the system class I call something like this:
echo Boot::getFilePath();
and should be displayed directories/ but I see again the default value: directory.
Now I though that this issue is related to the static field, but how can I access to the changed value so? Thanks.
Class variables defined with and without static are different variables.
One solution is to remove static from variable declaration and change getPath code, as you already have instance of Boot defined witn new:
class Boot
{
private $_filePath = 'directory/';
public function __construct()
{
require 'system.php';
}
public function init()
{
new System();
}
public function setFilePath($newDir)
{
$this->_filePath = $newDir;
}
public function getFilePath()
{
return $this->_filePath;
}
}
And call getFilePath() as
echo $b->getFilePath();
Another solution is to change both setFilePath and getFilePath:
public function setFilePath($newDir)
{
// set STATIC variable
self::$_filePath = $newDir;
}
public static function getFilePath()
{
// get STATIC variable
return self::$_filePath;
}
But in the end it's a bad approach because you will make mistakes deciding whether you need to access a static variable or a property of an object.
So it's better to make a decision - either you have an instance of Boot and get properties of it or you have only static methods in a class and forget about Boot instance.
Parent Class
class admarvel_generic_network
{
protected $attributeSettings;
public function __construct()
{
$this->attributeSettings = "something";
}
public static function parentGetAd()
{
print_r($this->attributeSettings); //throws FATAL ERROR Using $this when not in object context
}
}
Child Class - Initiating object of same class within static function
class Agencies_selectablemedia_test extends admarvel_generic_network
{
public static function getAd($frengoAdParams)
{
$adnw = new Agencies_selectablemedia_test();
$ad = $adnw->parentGetAd();
return $ad;
}
}
//Entry point
$ad_contents = Agencies_selectablemedia_test::getAd($params);
echo $ad_contents;
I get a fatal error, as highlighted in the code above.
I checked that if I make the following changes in child and parent class -
Parent Class
public static function parentGetAd($obj)
{
print_r($obj->attributeSettings); //this works
}
Child Class
public static function getAd($frengoAdParams)
{
$adnw = new Agencies_selectablemedia_test();
$ad = admarvel_generic_network::parentGetAd($adnw); //used scope resolution operator and passed object as parameter.
return $ad;
}
Could someone explain the above? I would like to understand why I cannot use $this->attributeSettings in the parent class's parentGetAd() function.
The reason why you cannot access $this->attributeSettings is that your are in a static method. So you aren't in the context of an object.
public static function parentGetAd($obj)
If you are declaring the method like this
public function parentGetAd($obj) {
}
you should be able to access $this->attributeSettings.
I am trying to create a simple MVC my personal use and I could really use an answer to this simple question
class theParent extends grandParent{
protected $hello = "Hello World";
public function __construct() {
parent::__construct();
}
public function route_to($where) {
call_user_func(array("Child", $where), $this);
}
}
class Child extends theParent {
public function __construct() {
parent::__construct();
}
public function index($var) {
echo $this->hello;
}
}
$x = new theParent();
$x->route_to('index');
Now Child::index() this throws a fatal error: Using $this when not in object context but if I were to use echo $var->hello, it works just fine.
I know I can use $var to access all properties in the parent, but I would rather use $this.
By writing call_user_func(array("Child", $where), $this) you are calling the method statically. But as your method isn't static you need some kind of object instance:
call_user_func(array(new Child, $where), $this);
Documentation on callback functions.
You don't have an instance of Child to call a non-static method upon when you're doing $x->route_to('index'); The way you're calling the method, without having made an instance first, is implied static.
There are two ways to correct it. Either make the Child class's methods static:
class Child extends theParent {
public function __construct() {
parent::__construct();
}
static public function index($var) {
echo self::$hello;
}
}
...or make an instance of the child class for the parent to use:
class theParent extends grandParent{
protected $hello = "Hello World";
private $child = false
public function __construct() {
parent::__construct();
}
public function route_to($where) {
if ($this->child == false)
$this->child = new Child();
call_user_func(array($this->child, $where), $this);
}
}
Of course, both of these samples are rather generic and useless, but you see the concept at hand.
$this gives you access to everything visible/accessible in the current object. That can either be in the class itself (this) or any of it's parents public or protected members/functions.
In case the current class overrides something of a parent class, you can access the parent method explicitly using the parent keyword/label, whereas you add :: to it regardless if it is not a static method.
Protected variables exist only once, so you can not use parent to access them.
Is this info of use?
Is it ok to put factory() method to the factoried object's class?
class User {
public static function factory($id) {
return new User($id);
}
private function __construct($id) {
}
}
And when consider placing factory() method into separated class?
class User {
public function __construct($id) {
}
}
class UserFactory {
public static function factory($id) {
return new User($id)
}
}
I can't see any benefits of using additional class for factory, but I consider that there are some benefits I don't know about. :)
When to put factory() method into factoried object and when put factory() method to separated class?
The advantage with putting the factory method inside the class itself is protecting the class from being instantiated without using the factory method:
class User {
public static function factory($id) {
return new User($id);
}
private function __construct($id) {
// Now, only the factory method within this class can call this method.
// (Additionally, this method was static, which it shouldn't.)
}
}
I let other add to this with advantages of the opposite solution.
If you have a static creator method there is not much use in putting in into a factory.
It's only really useful to put factory method in it's own class if it isn't static and you want to inject it somewhere.
class User {
public static function __construct($id) {
}
}
class UserFactory {
public function factory($id) {
return new User($id)
}
}
class SomethingUserReleated {
public function __construct(UserFactory $factory) {
$this->userFactory = $factory;
}
public function iNeedToCreateAnUserForSomething() {
$userOne = $this->userFactory->factory(1233);
$userTwo = $this->userFactory->factory(123533);
}
}
Since you can't to the above with static methods.
Moving the factory methods into separate class allows you to separate object-methods and factory-specific methods (that are only needed while creating a new object).
class User {
public static function __construct($id, $name){
// call this directly or via Factory
}
}
class UserFactory {
private static function randomName(){
// return some random name
}
public static function factory($id){
return new User($id, UserFactory::randomName());
}
}