Here is a simplifed
I know the code that follows, is not perfectly clean, but for test
Code1:
<?PHP
abstract class webservice
{
protected $url;
var $clientSoap;
public function affectation_base($url_p)
{
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$this->url=$url_p;
$clientSoap = new SoapClient('wdsl_adress');
}
public function get_fonction()
{
$clientSOAP = new SoapClient('wdsl_adress');
$sestruct = new stdClass();
$sestruct->value = "test";
var_dump($clientSOAP->MD5($sestruct));
}
abstract protected function getValue();
}
class Webservice_2 extends webservice
{
public function __construct($url_p)
{
$this->affectation_base($url_p);
}
function getValue()
{}
}
$wbs = new Webservice_2('wdsl_adress');
$wbs->getValue();
$wbs->get_fonction();
?>
Code2:
<?PHP
abstract class webservice
{
protected $url;
var $clientSoap;
public function affectation_base($url_p)
{
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$this->url=$url_p;
$clientSoap = new SoapClient('wdsl_adress');
}
public function get_fonction()
{
$sestruct = new stdClass();
$sestruct->value = "test";
var_dump($clientSOAP->MD5($sestruct));
}
abstract protected function getValue();
}
class Webservice_2 extends webservice
{
public function __construct($url_p)
{
$this->affectation_base($url_p);
}
function getValue()
{}
}
$wbs = new Webservice_2('wdsl_adress');
$wbs->getValue();
$wbs->get_fonction();
?>
"Code1" works
"Code2" doesn't work:
PHP Fatal error: Call to a member function MD5() on a non-object in E:\test.php on line 20
Line 20 is the var_dump(); line
I don't understand why use $clientSOAP->MD5 is a problem
What is the correct solution ?
Thanks in advance
Ps:excuse me if I speak very well English, this isn't my language
The right code for number 2 is :
public function get_fonction()
{
$sestruct = new stdClass();
$sestruct->value = "test";
var_dump($this->clientSOAP->MD5($sestruct));
}
because the $clientSOAP variable is not defined as in the code n° 1
Related
I need to send and get Class into another class like a variable in MVC method :
class Demo {
private $vars = array();
function set($var , $data) {
$this->vars[$var] = $data;
}
function request() {
extract($this->vars);
var_dump($vars);
}
}
And i want to use above class into this :
class Test extends foo {
function __construct(){
$this->demo = new Demo();
}
function register(){
$user = Load::model('user');//Now User Is An Object
$this->demo->set('user',"$user");//This Is Error
$this->demo->request();
}
Catchable fatal error: Object of class user could not be converted to string in
remove the quotes
$this->demo->set('user', $user);
And don't forget to create your variable in your class scope
class Test extends foo {
public $demo;
should do the trick
class Test extends foo {
protected $demo; // you missed to declare this variable
function __construct(){
$this->demo = new Demo();
}
function register() {
$user = Load::model('user');//Now User Is An Object
$this->demo->set('user', $user);
$this->demo->request();
}
}
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';
}
}
Im testing this thing where i'm trying to load a class and use it like this:
$this->model->model_name->model_method();
This is what I've got:
<?php
error_reporting(E_ALL);
class Loader {
public function model($model)
{
require_once("models/" . $model . ".php");
return $this->model->$model = new $model;
}
}
class A {
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->load->model('Test');
$this->text = $this->model->Test->test_model();
}
public function get_text()
{
return $this->text;
}
}
$text = new A();
echo $text->get_text();
?>
Im getting a bunch of errors here:
Warning: Creating default object from empty value in
C:\xampp\htdocs\fw\A.class.php on line 9
Notice: Trying to get property of non-object in
C:\xampp\htdocs\fw\A.class.php on line 24
Fatal error: Call to a member function test_model() on a non-object in
C:\xampp\htdocs\fw\A.class.php on line 24
What am I doing wrong? Thanks for any tip!
P.S. not much in the loaded file:
<?php
class Test {
public function test_model()
{
return 'testmodel';
}
}
?>
In the A class' constructor you are not assigning the "loaded" model to anything and later you are trying to use the $model property which has nothing assigned to it.
Try this:
class A {
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->model = $this->load->model('Test');
$this->text = $this->model->test_model();
}
(...)
Problem may be that you have not defined Loader.model as object but treating it like it is.
class Loader {
public $model = new stdClass();
public function model($model)
{
require_once("models/" . $model . ".php");
return $this->model->$model = new $model();
}
}
When you have your class like this you can use
$this->model->model_name->model_method();
Try the following code(UPDATED) if you want to avoid $this->model = $this->load->model('Test') in the constructor.
You can simply load the models by calling $this->loadModel(MODEL) function
<?php
error_reporting(E_ALL);
class Loader {
private $models = null;
public function model($model)
{
require_once("models/" . $model . ".php");
if(is_null($this->models)){
$this->models = new stdClass();
}
$this->models->$model = new $model();
return $this->models;
}
}
class A{
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->loadModel('Test');
$this->loadModel('Test2');
$this->text = $this->model->Test2->test_model();
}
public function get_text()
{
return $this->text;
}
private function loadModel($class){
$this->model = $this->load->model($class);
}
}
$text = new A();
echo $text->get_text();
?>
I'm writing a class and I can't figure out why I am getting this error:
PHP Fatal error: Call to undefined method Directory::BuildDirectoryListing()
in C:\www\directory.php on line 25
It doesn't make any sense. By the error it looks like it is trying to look for a static function. Here is the code I am using:
$odata = new Directory($listing['id']);
$adata = $odata->BuildDirectoryListing();
<?php
include_once("database.php");
class Directory {
public $listing = array();
public $aacategories = array();
function __construct($_listing) {
$this->listing = $_listing;
}
public function BuildDirectoryListing() {
/* function code here */
}
}
?>
Directory is a PHP built-in class.
You need to namespace your code or change your class name:
Class:
<?php
namespace MyApp;
class Directory {
public $listing = array();
public $aacategories = array();
function __construct($_listing) {
$this->listing = $_listing;
}
public function BuildDirectoryListing() {
/* function code here */
}
}
?>
Creating the class:
<?php
$odata = new \MyApp\Directory($listing['id']);
$adata = $odata->BuildDirectoryListing();
?>
You are calling a static function in Directory::BuildDirectoryListing(), change this line
public function BuildDirectoryListing() {
for
public static function BuildDirectoryListing() {
Here is my class that gets called on each page:
class ActionHandler {
var $smarty = NULL;
public function __construct() {
if($this->smarty == NULL){
$this->smarty = new Smarty();
$this->smarty->template_dir = TEMPLATE_DIR;
$this->smarty->compile_dir = COMPILE_DIR;
}
}
public function do_something($page_id) {
return $page_id + 1;
}
}
Now I have a custom plugin for smarty that I want to use in my template:
function smarty_function_something($params, &$smarty) {
return ActionHandler::do_something($params['page_id']);
}
However I get Fatal error: Using $this when not in object context.
I see why but don't know how to get around this. Any ideas?
Try making the do_something a static member of ActionHandler
class ActionHandler {
public static $smarty = NULL;
public function __construct()
{
if($this->smarty == NULL)
{
$this->smarty = new Smarty();
$this->smarty->template_dir = TEMPLATE_DIR;
$this->smarty->compile_dir = COMPILE_DIR;
}
}
public static function do_something($page_id)
{
return $page_id + 1;
}
}
As your trying to access a non static method i *think that the __construct gets executed before the method is available, but as you have not created an instance of the object, the keyword $this does not exists.
you have to create specific static methods. if your going MyObject::SomeMethod($param)
you should also take a look at Object Auto Loading and Auto Initializing objects via static methods.
also you don't need to specifically define the value to public static $smarty = NULL; as Null is a default value of any new variable, just do
public static $smarty;
going a little more indepth with your problem you should add a singleton method like so..
class ActionHandler
{
public static $smarty;
public static $singleton;
public function __construct()
{
if($this->smarty == NULL)
{
$this->smarty = new Smarty();
$this->smarty->template_dir = TEMPLATE_DIR;
$this->smarty->compile_dir = COMPILE_DIR;
}
}
public static GetSingleton()
{
if(self::$singleton == null)
{
self::$singleton = new ActionHandler();
}
return self::$singleton;
}
public static function do_something($page_id)
{
$_this = self::GetSingleton();
return $page_id + 1;
}
}
You omitted a few pieces of code: instantiation of either the Smarty or ActionHandler object, registration of the template function, template content, and Smarty::display() call, but in my own testing your code works fine. In none of your code do you attempt to use $this while not in an object context.
If you have additional code to post (preferably, the full reduction that still triggers the error) that may help with debugging.
smarty-test.php:
<?php
include 'Smarty.class.php';
class ActionHandler {
var $smarty = NULL;
public function __construct() {
if($this->smarty == NULL){
$this->smarty = new Smarty();
$this->smarty->template_dir = __DIR__ . '/t';
$this->smarty->compile_dir = __DIR__ . '/tc';
$this->smarty->plugins_dir = __DIR__ . '/plugins';
}
}
public function do_something($page_id) {
return $page_id + 1;
}
}
$ah = new ActionHandler;
$ah->smarty->display('index.tpl');
plugins/function.something.php:
<?php
function smarty_function_something($params, &$smarty) {
return ActionHandler::do_something($params['page_id']);
}
t/index.tpl:
Test: {something page_id=1}
Output:
Test: 2