Wordpress: Overriding a function in a derived class - php

I need to override the function adforest_profile_full_top() from profile.php. This function is in a class called adforest_profile.
I added this code in my function.php:
add_action( 'after_setup_theme', function() {
class Myclass extends adforest_profile {
function adforest_profile_full_top() {
//original function code with my custom modifications
}
}
new Myclass();
});
With this, I received the following error:
Parse error syntax error unexpected '$user_pic'
This is the parent PHP file that contains the function I want to override:
<?php
if (!class_exists('adforest_profile')) {
class adforest_profile {
// user object
var $user_info;
public function __construct() {
$this->user_info = get_userdata(get_current_user_id());
}
// Full Width Profile Top
function adforest_profile_full_top() {
$user_pic = adforest_get_user_dp($this->user_info->ID, 'adforest-user-profile');
//and more code...
}
}
}
?>
Can you give me a good way to override this function in my function.php file? I don't want to override the entire class, just one function inside the class. Thank you.

Related

How To Pass Arguments To new Class & Define Variables

I'm trying to modify the plugin function in the original PHP class named My_Widget_Admin which i have copied over from a plugin to my theme, but get Fatal error Too few arguments to function My_Widget_Admin
Here is the code i added in my theme :
class Custom_Admin extends My_Widget_Admin {
function item_select() {
// Code
}
}
$new = new Custom_Admin;
$new->item_select();
I think it has something to do with this code from the plugin :
private $_instance;
private $_widget_object;
function __construct( $instance, $widget_object ) {
$this->_instance = $instance;
$this->_widget_object = $widget_object;
$this->form();
}
I need to pass these 2 arguments $instance, $widget_object to the new function Custom_Admin.
How do i do that?
Passing arguments to a constructor must be done when the class gets instantiated. This is done with the new keyword.
class Custom_Admin extends My_Widget_Admin {
function item_select() {
// Code
}
}
$new = new Custom_Admin($instance, $widget_object);
$new->item_select();

Set a non-string variable in a function in a class to get in other PHP files class

Firstly:
On main.php I can use $this->tester1 to share the variable between functions, therefore do I need the public $tester1; at the top of my file - it seems to work fine without it?
Secondly:
I am attempting to get the variable set in main.php on other.php, if I remove $this->tester1 = 'test'; from the function and set public $tester1 to = 'test'; then echo $this->tester1; on either file will output fine.
However instead of 'test' I need the variable to be = get_option( 'my_option' ); which needs to be called after the __construct on the init action, and therefore needs to be within the function mymainfunction.
If I try to set the variable in that function and try to access it via other.php it says unexpected PUBLIC, if I remove public from the variable then the output is Undefined property: MyMainClass::$tester1
The output of the get_option is a line of text.
I think I am nearly there with this but missing something simple - hopefully! :)
Here are my files:
main.php
class MyMainClass {
public $tester1;
public function __construct() {
add_action( 'init', array( &$this, 'mymainfunction' ) );
}
public function mymainfunction() {
$this->tester1 = 'test';
echo $this->tester1;
}
}
other.php
class MyOtherClass {
public function __construct() {
add_action( 'init', array(&$this, 'myotherfunction' ) );
}
public function myotherfunction() {
require_once 'main.php';
$getvariables = new MyMainClass();
echo 'tester1 is:' . $getvariables->tester1;
}
}
Firstly:
If I'm not wrong, declaration without any explicit visibility keyword are defined as public.
Secondly:
You are including a class in the middle of a method of another one.. Seems quite strange to me.. "require_once" just insert the content of a file, where the instruction is called. So my guess is that it's mess up with PHP syntax. In order to try to help you, would you try to replace your "other.php" by something like :
require_once 'main.php';
class MyOtherClass {
public function __construct() {
add_action( 'init', array(&$this, 'myotherfunction' ) );
}
public function myotherfunction() {
$getvariables = new MyMainClass();
echo 'tester1 is:' . $getvariables->tester1;
}
}
But to be honest, I don't know what you are trying to do, but something like this would be simplier, right ?
main.php
class MyMainClass {
public $tester1;
public function __construct() {
$this->tester1 = 'test1';
}
}
other.php
require_once 'main.php';
class MyOtherClass {
public function __construct() {
$main = new MyMainClass();
echo 'tester1 is:' . $main->tester1;
}
}

Phalcon library class calling a function within another

Im using phalcon 2.0.0 and i am trying to call a function with in another function but from the same class like shown below, for some reason i get a blank page. And when i comment the calling of 2nd function from first, the page loads properly.
<?php
use Phalcon\Mvc\User\Component;
class Testhelper extends Component {
public function f1($data) {
$tmp = $this->f2($data);
return $tmp;
}
public function f2($data) {
return '5'; // just testing
}
}
And btw im accessing the f1 function by the volt function extender like this
$compiler->addFunction('customfunc', function($resolvedArgs, $exprArgs) {
return 'Testhelper ::f1('.$resolvedArgs.')';
});
if someone could help me, it would be deeply appreciated.
Thanks guys
You are trying to call TestHelper f1() statically in Volt, where your class does not expose that function as a static.
You can change your code like this:
<?php
use Phalcon\Mvc\User\Component;
class Testhelper extends Component
{
public static function f1($data)
{
$tmp = self::f2($data);
return $tmp;
}
public static function f2($data)
{
return '5'; // just testing
}
}
and your Volt function will work. However you have to bare in mind that because you are calling things statically you won't have immediate access to all the di container services that the Component offers like so:
$this->session
$this->db
You will need to modify your code to pick the di container using the getDefault()
Another option is to use the code as you have right now, but register the TestHelper in your di container like so:
$di->set(
'test_helper',
function () {
return new TestHelper();
}
);
and then your volt function will need to change to:
$compiler->addFunction(
'customfunc',
function ($resolvedArgs, $exprArgs) {
return '$this->test_helper->f1('.$resolvedArgs.')';
}
);

using same namespace php, Call to undefined function

using same namespace php
I have this files in the same folder :
OtherFunctions.php
<?php
namespace Pack\sp;
$Tble = NULL;
function SetTble($tble) {
global $Tble;
$Tble = $tble;
}
function GetTble() {
global $Tble;
return $Tble;
}
function Funct0($Str0, $Str1) {
return $Str0 == $Str1;
}
function Funct1($Arg) {
return "The Value is ".$Arg;
}
//... from 0 to 16
function Funct16($Arg) {
return "The Value is ".$Arg;
}
?>
How to call all functions contained in this file?
In one class File SubClass.php I have this:
<?php
namespace Pack\sp;
class SubClass {
public $CArg = "";
}
?>
In other class File LeadClass.php
I have this:
<?php
namespace Pack\sp;
use \Pack\sp\SubClass;
require_once("OtherFunctions.php");
class LeadClass {
public function __construct($Name) {
echo("_._");
$NewSC = new SubClass();
$NewSC->CArg = $Name;
SetTble($Name);
echo("ini:".GetTble().":end");
}
}
?>
I want call all function in one instruction of OtherFunctions.php File, but I don't kno how to do it....
I trying to replicate this message in other code
Fatal error: Call to undefined function GetTble() in C:...\LeadClass.php on line 10
But, I'm obtaining blank page
EDIT
Was added the line:
require_once("OtherFunctions.php");
And was replaced the line:
require_once("SubClass.php");
by the line:
use \Pack\sp\SubClass;
in LeadClass.php File.
But, I'm obtaining blank page
You need to add the next line
namespace Pack\sp;
use \Pack\sp\SubClass; // <--- add this
Also I think you should put the functios of the OtherFunctions file into a new class link
namespace Pack\sp;
class OtherFunctions{
// your current code goes here
}
After that you need to extend the SubClass whit the OtherFunctios class
namespace Pack\sp;
use Pack\sp\OtherFunctions;
class SubClass extends OtherFunctions {
public $CArg = "";
}
EDIT
I just tried your code and I can make the LeasClass to work as follow
<?php
namespace Pack\sp;
require_once("OtherFunctions.php");
require_once("SubClass.php");
class LeadClass {
public function __construct($Name) {
echo("_._");
$NewSC = new SubClass();
$NewSC->CArg = $Name;
SetTble($Name);
echo("ini:".GetTble().":end");
}
}
$LeadClass = new LeadClass('table');
?>
Have you already initialize the class?

How to include a php and then remove it?

Well, I don't know if this post have the correct title. Feel free to change it.
Ok, this is my scenario:
pluginA.php
function info(){
return "Plugin A";
}
pluginB.php
function info(){
return "Plugin B";
}
Finally, I have a plugin manager that is in charge of import all plugins info to pool array:
Manager.php
class Manager
{
protected $pool;
public function loadPluginsInfo()
{
$plugin_names = array("pluginA.php", "pluginB.php");
foreach ($plugin_names as $name)
{
include_once $name;
$this->pool[] = info();
}
}
}
The problem here is that when I print pool array it only show me the info on the first plugin loaded. I supposed that the file inclusing override the info because it still calling the info() method from the first include.
Is there a way to include the info of both plugins having the info() function with the same name for all plugins files?
Thank you in advance
PS: a fatal cannot redeclare error is never hurled
you can use the dynamic way to create plugin classes
plugin class
class PluginA
{
public function info()
{
return 'info'; //the plugin info
}
}
manager class
class Manager
{
protected $pool;
public function loadPluginsInfo()
{
$plugin_names = array("pluginA", "pluginB"); //Plugin names
foreach ($plugin_names as $name)
{
$file = $name . '.php';
if(file_exists($file))
{
require_once($file); //please use require_once
$class = new $name(/* parameters ... */); //create new plugin object
//now you can call the info method like: $class->info();
}
}
}
}
Are you sure the interpreter isn't choking w/ a fatal error? It should be since you're trying to define the info function twice here.
There are many ways to achieve what you want, one way as in #David's comment above would be to use classes, eg.
class PluginA
{
function info() { return 'Plugin A'; }
}
class PluginB
{
function info() { return 'Plugin B'; }
}
then the Manager class would be something like this:
class Manager
{
protected $pool;
public function loadPluginsInfo()
{
$plugin_names = array("PluginA", "PluginB");
foreach ($plugin_names as $name)
{
include_once $name . '.php';
$this->pool[] = new $name();
}
}
}
Now you have an instance of each plugin class loaded, so to get the info for a plugin you would have $this->pool[0]->info(); for the first plugin. I would recommend going w/ an associative array though so you can easily reference a given plugin. To do this, the assignment to the pool would become:
$this->pool[$name] = new name();
And then you can say:
$this->pool['PluginA']->info();
for example.
There are many other ways to do it. Now that 5.3 is mainstream you could just as easily namespace your groups of functions, but I would still recommend the associative array for the pool as you can reference a plugin in constant time, rather than linear.

Categories