Undefined Function Error PHP - php

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() {

Related

Class autoload not working

I'm a beginner in PHP development and I'm facing a problem in my development in PHP OO. I saw is better use the autoload() function than include each file of PHP Class.
My doubt is: Why my autoload function does not work?
Follow bellow my code:
<?php
function __autoload($class)
{
include_once "model/{$class}.class.php";
}
$avaliacaoLocal = new AvaliacaoLocal();
$avaliacaoLocal->setId(1);
$avaliacaoLocal->setIdLocal(2);
$avaliacaoLocal->setComentarios("Comentários de Pedro");
$avaliacaoLocal->setIdPessoaCliente(3);
$avaliacaoLocal->setValor(5);
var_dump($avaliacaoLocal);
File AvaliacaoLocal.class.php
<?php
namespace model;
class AvaliacaoLocal
{
private $id;
private $valor;
private $comentarios;
private $idLocal;
private $idPessoaCliente;
public function __construct(){
$this->clear();
}
public function clear(){
$this->id = 0;
$this->valor = 0;
$this->comentarios = "";
$this->idLocal = null;
$this->idPessoaCliente = null;
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getValor()
{
return $this->valor;
}
public function setValor($valor)
{
$this->valor = $valor;
}
public function getComentarios()
{
return $this->comentarios;
}
public function setComentarios($comentarios)
{
$this->comentarios = $comentarios;
}
public function getIdLocal()
{
return $this->idLocal;
}
public function setIdLocal($idLocal)
{
$this->idLocal = $idLocal;
}
public function getIdPessoaCliente()
{
return $this->idPessoaCliente;
}
public function setIdPessoaCliente($idPessoaCliente)
{
$this->idPessoaCliente = $idPessoaCliente;
}
}
The error:
PHP Fatal error: Class 'AvaliacaoLocal' not found in C:\Users\Pedro
........\index.php on line 14
UPDATE:
When i use include the PHP returns the same error:
Fatal error: Class 'AvaliacaoLocal' not found in C:\Program
Files\VertrigoServ\www\system\index.php on line 10
i've change folder to verify if could be it.
The class is declared belonging to a namespace, you have to call it in this way:
$avaliacaoLocal = new \model\AvaliacaoLocal();
But now, the namespace is also included in $class, so the autoload function needs to handle that:
function __autoload($class)
{
$file = str_replace(array('_', '\\'), '/', $class) . '.php';
if (is_file($file)) {
require $file;
}
}
This function takes $class value and replace every \ (and _) from the namespace with a / to get the file name.

Variable as static function

I have a class Sections:
class Sections {
public static function get($name) {
//
}
}
And I would like to call the static function get() from it with a variable (http://php.net/manual/en/functions.variable-functions.php):
$section = 'Sections::get';
$section('body');
But it gives me a Fatal error: Call to undefined function Sections::get()
Is it possible to call a static function in this way?
Thanks!
You need to store the class separately from the method:
$class = 'Sections';
$method = 'get';
Now you can call it like this:
$class::$method('body');
Try using call_user_func to do this:
$section = array('Sections', 'get');
call_user_func($section, 'body');
Or:
$section = 'Sections::get';
call_user_func($section, 'body');
I've solved it with the help of a function outside of the class:
class Sections {
public static function get($name) {
//
}
}
function section($name) {
Sections::get($name);
}
Now I can do:
$section = 'section';
$section('body');

why does this class return variable not found?

Why am i getting
Undefined variable: req
When i have declared the property at the top of my class:
<?php
namespace Craft;
class Disqus_ApiService extends BaseApplicationComponent
{
private $req = false;
public function init()
{
$d = craft()->plugins->getPlugin('disqus');
$settings = $d->getSettings();
$this->$req = new \DisqusAPI($settings['DISQUS_SECRET_KEY']);
}
public function trends()
{
return $this->req->trends;
}
}
Use
$this->req
Instead of
$this->$req
In
$this->$req = new \DisqusAPI($settings['DISQUS_SECRET_KEY']);

class structure using SoapClient

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

Notice: Undefined index: in class->array

This is the code:
class app {
public $conf = array();
public function init(){
global $conf;
$conf['theme'] = 'default';
$conf['favicon'] = 'favicon.ico';
}
public function site_title(){
return 'title';
}
}
$app = new app;
$app->init();
//output
echo $app->conf['theme'];
And I get this error:
Notice: Undefined index: theme in C:\xampp\htdocs\...\trunk\test.php on line 21
Where have I gone wrong, and is there any simpler way to get same result?
You are in the wonderful world of OOP, no longer do you have to use global!
Try this:
class app
{
public $conf = array();
// Notice this method will be called every time the object is isntantiated
// So you do not need to call init(), you can if you want, but this saves
// you a step
public function __construct()
{
// If you are accessing any member attributes, you MUST use `$this` keyword
$this->conf['theme'] = 'default';
$this->conf['favicon'] = 'favicon.ico';
}
}
$app = new app;
//output
echo $app->conf['theme'];
You are populating a separate global variable instead of the object properties. Use $this:
class app {
public $conf = array();
public function init(){
$this->conf['theme'] = 'default';
$this->conf['favicon'] = 'favicon.ico';
}
public function site_title(){
return 'title';
}
}
$app = new app;
$app->init();
//output
echo $app->conf['theme'];

Categories