I have this class:
class Search
{
protected static $Basics;
public function __construct() {
self::$Basics = new Basics();
}
public static function getT() {
return self::$Basics->get('keywords/t');
}
public static function isAvailable($keyword) {
return self::$Basics->get('keywords/available', ['keyword' => $keyword])['available'];
}
}
The class Basics is really simple class:
class Basics
{
public function __construct()
{
//some code..
}
public function get($keyword, $param = null)
{
return ['available' => true];
}
}
Call to getT function:
use App\Libraries\Search;
class GV
{
public function test() {
echo Search::getT() ? 'ok' : 'bad';
}
}
But, when i run the function getT in class Search, it return this error: Call to a member function get() on a non-object
What can i do?
You are calling the method inside Search statically (Search::getT();) which will never fire the __construct() method.
__construct() gets fired upon instantiating the class ($search = new Search;), not upon calling static methods (Class::method();).
Simply instantiate your search object: $search = new Search;
Like so:
use App\Libraries\Search;
class GV
{
public function test() {
$search = new Search;
echo $search::getT() ? 'ok' : 'bad';
}
}
Related
I have notification classes called ProductNotification, OrderNotification, etc, these have a mail method which returns another class which holds further data for the sending of emails:
class ProductNotification {
public function mail()
{
return ProductMail::class;
}
}
class OrderNotification {
public function mail()
{
return OrderMail::class;
}
}
Is there a way to instantiate the ProductMail class from the method, the following doesn't work and I'm not sure how to pass through another variable $data to the constructo.?
class BaseNotification {
public function toMail()
{
return (new $this->mail())->to($email)->send();
{
}
I know that if mail() was a property on the class instead, that this would be possible and I can pass through $data to the constructor as the following works, but is this possible from a method?
class ProductNotification {
public $mail = ProductMail::class;
}
class BaseNotification {
public function toMail()
{
return (new $this->mail($data))->to($email)->send();
{
}
You can store the class as a local variable in the toMail method and then instantiate it.
class BaseNotification {
public function toMail($data)
{
$mail_class = $this->mail();
return new $mail_class($data);
}
}
You can't instantiate an object from a method call, but you can do so from a variable. So just assign the method's return call to a variable:
class ProductNotification extends BaseNotification {
public function mail() {
return ProductMail::class;
}
}
class OrderNotification extends BaseNotification {
public function mail() {
return OrderMail::class;
}
}
class BaseNotification {
public function toMail()
{
$data = [];
$class = $this->mail();
return (new $class($data))->to($email)->send();
{
}
Though it might be cleaner to just use the mail() method to build the mail:
class ProductNotification extends BaseNotification {
public function mail($data) {
return new ProductMail($data);
}
}
class OrderNotification extends BaseNotification {
public function mail($data) {
return new OrderMail($data);
}
}
class BaseNotification {
public function mail() {
throw new \Exception("not implemented");
}
public function toMail()
{
$data = [];
return $this->mail($data)->to($email)->send();
{
}
I think it is very basic functionality, please help.
How can I call non-static method into static-method in php.
class Country {
public function getCountries() {
return 'countries';
}
public static function countriesDropdown() {
$this->getCountries();
}
}
Preferred way..
It is better to make the getCountries() method static instead.
<?php
class Country {
public static function getCountries() {
return 'countries';
}
public static function countriesDropdown() {
return self::getCountries();
}
}
$c = new Country();
echo $c::countriesDropdown(); //"prints" countries
Adding a self keyword displays the PHP Strict Standards Notice To avoid that you can create an object instance of the very same class and call the method associated with it.
Calling a non-static method from a static method
<?php
class Country {
public function getCountries() {
return 'countries';
}
public static function countriesDropdown() {
$c = new Country();
return $c->getCountries();
}
}
$c = new Country();
echo $c::countriesDropdown(); //"prints" countries
You even use Class Name
public static function countriesDropdown() {
echo Country::getCountries();
}
You cannot straight forward do that for that you need create a instance of the class & have to call the non-static method,
class Country {
public function getCountries() {
return 'countries';
}
public static function countriesDropdown() {
$country = new Country();
return $country->getCountries();
}
}
DEMO.
I have this code and i´m trying to use a object
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
?>
i would like to do this:
class Index extends Controller {
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
i would like to use the set_parms function.
but i can't see the View Function, then i can not use.
Can someone explain and advise me a good and safe way?
Correction from Phil: If a __construct() method isn't found, PHP will revert to legacy constructor syntax and check for a method with the same name as the object. In your case the method index() is being treated as the constructor, and is preventing the parent's constructor from loading the view object into the $_view property.
You can force a class to inherit a parent's constructor by defining __construct() in the child and calling the parent's constructor:
public function __construct() {
parent::_construct();
}
Here is the fixed code:
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
.
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
.
class Index extends Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
How to display a class from another class in PHP ?
class Layout {
public $var;
public function __construct() {
$this->var = 'test';
}
public function __toString() {
return $this->var;
}
}
class Template {
private $var_layout;
public function __construct() {
$obj = new Layout;
$this->var_layout = $obj;
}
public function __toString() {
return $this->var_layout;
}
}
$template = new Template();
echo($template);
Error message: Method Template::__toString() must return a string value
Please help, thank you very much..
return $this->var_layout; in the Template class does not return a string, it returns an object. Make it return a string by calling the __toString() method of that object explicitly.
I would like to write a generic method that refers to a generic class (but the same method) in php.
class A {
public static function Dox(){
}
}
class B {
public static function Dox(){
}
}
class C{
public static function Include($class){
$result = $class::Dox(); //instead of 2 methods => A::Dox and B::Dox
}
}
I get an error.
any suggestions?
include is a keyword. Rename your method to foo(), bar() or anything that is not a keyword.
e.g.
<?php
class A {
public static function Dox() { return 'A::Dox'; }
}
class B {
public static function Dox() { return 'B::Dox'; }
}
class C {
public static function foo($class) {
$result = $class::Dox();
echo 'result: ', $result, "\n";
}
}
foreach( array('A','B') as $c ) {
C::foo($c);
}
prints
result: A::Dox
result: B::Dox
Does call_user_func work?
class A {
public static function Dox() {
}
}
class B {
public static function Dox() {
}
}
class C {
public static function Include($class) {
$result = call_user_func(array($class, "Dox"));
}
}
Include keyword "spesific keyword".
Try it:
public static function IncludeXXX(){...}