A im creating some custom widget in Word Press, but what i need is that i can pass GET value inside my widget, i have tried many ways but no luck, here is my widget code in WP for now
class Trucks extends WP_Widget{
function __construct(){
parent::__construct(false, $name = __('Truck API'));
}
function form(){
}
function update(){
}
function widget($args, $instance){
extract($args, EXTR_SKIP);
if(isset($_GET['a'])) {
$test = $_GET['a'];
echo $test;
}
else {
$test = 'Nothing';
}
}
}
add_action('widgets_init', function(){
register_widget('Trucks');
});
?>
It diplay for now nothing, it is much more complex class, but this is made simple to understand what i need :)
Related
Hi Please see the following code of my class
<?php
class MyClass
{
function __construct()
{
function test1()
{
echo 'hii- test1';
}
$this->autoload_function();
}
function autoload_function()
{
$my_array = array(
"test1"
);
foreach ($my_array as $my_function)
{
call_user_func($my_function);
}
}
}
$var = new MyClass();
?>
Here I have to add other values to $my_array and then I have to define other function outside this class . I have to do this via add_action. So I modified the code please see below
my-class.php
<?php
class MyClass
{
function __construct()
{
function test1()
{
echo 'hii- test1';
}
$this->autoload_function();
}
function autoload_function()
{
$my_array = array(
"test1"
);
apply_filters('modify_array', $my_array);
foreach ($my_array as $my_function)
{
call_user_func($my_function);
}
}
}
$var = new MyClass();
?>
my-newpage.php
add_action ('modify_array', 'modify_array_function', 0);
function modify_array_function($my_array){
array_push($my_array, 'test2')
}
function test2(){
echo 'hii- test2';
}
Here i can see that new value is added to array but i am not sure about where i can write my new function i am getting following error
Warning: call_user_func() expects parameter 1 to be a valid callback,
class 'MyClass' does not have a method
'test2'
I want to define test2() inside my-newpage.php and I am ready to add more action or filter inside my-class.php
I setup everything in a proper way that's why add_action ('modify_array', 'modify_array_function', 0); is working .
Please help.
Here i can see that new value is added to array
I want to define test2() inside my-newpage.php and I am ready to add
more action or filter inside my-class.php
If you're certain test2 successfully added to the array and that test2 was being called from MyClass::autoload_function(), then you can try using an early hook like init to define test2, like so:
// Defines the test2() function.
function my_define_test2() {
function test2(){
echo 'hii- test2';
}
}
// Now hook on init to ensure test2() is defined before your main plugin
// is loaded.
add_action( 'init', 'my_define_test2', 1 );
See my full answer here.
By using the following class:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
$trimmed=trim($form);
$specialchar=htmlspecialchars($trimmed);
$finaloutput=stripslashes($specialchar);
echo $finaloutput;
}
public function __destruct()
{
unset($finaloutput);
}
}
and Calling the function, by the following code, it works fine.
<?php
require('source/class.php');
$target="<script></script><br/>";
$forminput=new SafeGuardInput($target);
?>
But if in the SafeGuardInput class if I replace echo $finaloutput; with return $finaloutput; and then echo $forminput; on the index.php page. It DOES NOT WORK. Please provide a solution.
You can't return anything from a constructor. The new keyword always causes the newly created object to be assigned to the variable on the left side of the statement. So the variable you've used is already taken. Once you remember that, you quickly realise there is nowhere to put anything else that would be returned from the constructor!
A valid approach would be to write a function which will output the data when requested:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
}
public function getFinalOutput()
{
$trimmed = trim($this->form);
$specialchar = htmlspecialchars($trimmed);
$finaloutput = stripslashes($specialchar);
return $finaloutput;
}
}
Then you can call it like in the normal way like this:
$obj = new SafeGuardInput($target);
echo $obj->getFinalOutput();
class dir_exam
{
public $db_ruta;
function __construct($db_ruta)
{
$this->db_ruta=$db_ruta;
}
function veritas()
{
$aa="ok";
$xx="ok2";
return $aa;
return $xx;
}
function create_d()
{
$r=$this->veritas();
echo $r->$aa;
echo $r->$xx;
}
}
I have this class and i try execute funtion veritas inside function create_d, but i want show the value from function veritas as individual values, showing value in create_d for $aa and $xx, when execute finally the class
<?php
$a=new dir_exam("db_p");
echo $a->create_d();
?>
But i can´t get this finally, i don´t know if it´s not possible or what, this it´s my question, thank´s in advanced
You can't have 2 or more returns in a function.
For you use the vars $aa and $xx like OOP, you must create the 2 var in the class
class dir_exam
{
public $db_ruta;
public $aa; // <--
public $xx; // <--
}
After, you need change the function veritas to pass the value for your attributes
function veritas()
{
$this->aa="ok";
$this->xx="ok2";
}
Now in your function you can call like that:
function create_d()
{
$this->veritas();
echo $this->aa;
echo $this->xx;
}
I just want to ask if its possible to call variables on class to another page of the site. I have tried calling the function's name and inside the parenthesis. I included the variable found inside that function e.g:
<?php
$loadconv -> loadmsg($msgReturn);
echo $loadconv;
?>
But it didn't work.
Do you want something like this?
class Load
{
public $msgReturn;
__construct()
{
}
public function loadMsg($param)
{
$this->msgReturn = $param;
}
}
Then you could do
$loadConv = new Load();
$loadConv->loadMsg('just a string');
echo $loadConv->msgReturn; // 'just a string'
Same function name in different isolated classes is not allowed?
What am I doing wrong?
I reduced my real code to the minimum required to make some test.
Here it is:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
class confFunctions {
function getConf() {
function doWork() {
echo "I am from confFunctions!<br />";
}
doWork();
}
}
class thePage {
function loadPage() {
function doWork() {
echo "I am from thePage!<br />";
}
doWork();
}
}
// Start check.
echo "Checking...<br />";
$conf = new confFunctions();
$conf->getConf();
$page = new thePage();
$page->loadPage();
?>
The output is:
Checking...
I am from confFunctions!
Fatal error: Cannot redeclare doWork() (previously declared in /var/www/Test2/index.php:11) in /var/www/Test2/index.php on line 23
Renaming one of the shared-name functions makes all working well. That is, changing doWork to doWork1 in the second class, like this:
class thePage {
function loadPage() {
function doWork1() {
echo "I am from thePage!<br />";
}
doWork1();
}
}
gives correct results:
Checking...
I am from confFunctions!
I am from thePage!
Should not what is inside a class be visible only to that class, if not declared public?
By declaring a function in a function, you are actually declaring the second function into the global scope.
If you want your functions to be limited to the class scope, don't declare a function in another function, but rather declare them under each other.
Consider this code that declares a function in another function (in a class):
<?php
class MyFunctions {
function load() {
function doWork() {
echo "I am doing my work from global scope";
}
}
}
$mf = new MyFunctions();
$mf->load();
// $mf->doWork(); <-- won't work here
doWork(); // <-- this will work!
?>
Now consider this code that declares a function under another function (in a class).
<?php
class MyFunctions {
function load() {
//...
}
function doWork() {
echo "I am doing my work from class scope";
}
}
$mf = new MyFunctions();
// $mf->load(); <-- not really important anymore
$mf->doWork(); // <-- this will work now
// doWork(); <-- won't work here anymore
?>
Function scope is always namespace wide when declaring a named function.
You'll need to assign it to a variable to constrain it to a specific scope ($doWork = function() { }).
You seem to be going down an odd path though. Perhaps you should just use a private method?
Full example just to make it clear:
class confFunctions {
function getConf() {
$doWork = function() {
echo "I am from confFunctions!<br />";
};
$doWork();
}
}
class thePage {
function loadPage() {
$doWork = function() {
echo "I am from thePage!<br />";
};
$doWork();
}
}
I dont think you meant to nest the functions ? and your calling them from the global scope.
something like this is likely what you meant
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
class confFunctions {
function getConf() {
$this->doWork();
}
function doWork() {
echo "I am from confFunctions!<br />";
}
}
class thePage {
function loadPage() {
$this->doWork();
}
function doWork() {
echo "I am from thePage!<br />";
}
}
// Start check.
echo "Checking...<br />";
$conf = new confFunctions();
$conf->getConf();
$page = new thePage();
$page->loadPage();
?>
First guess would be that somehow you aren't properly closing your class from the first example. Different classes are definitely allowed to have the same function names, so there's something else going on in your code here that's not being shown through the psuedo-code you're posting.
UPDATE:
As NL-X said, by posting the function inside of a class function it then creates it in global scope. Thank you for updating your pseudo-code with actual examples.