In a working Drupal 8 site in THEME.theme I have
function fcx_preprocess_page(&$variables) {
$variables['get']['vguid'] = \Drupal\Component\Utility\XSS::filter($_GET['vguid']);
}
function fcx_preprocess_node(&$variables) {
$variables['get']['vguid'] = \Drupal\Component\Utility\XSS::filter($_GET['vguid']);
}
I have verified that the file core/lib/Drupal/Component/Utility/Xss.php exists with permissions 0664 and declares class XSS. In that file the method is declared public static function filter(...
When accessing pages which reference get.vguid I get error Class 'Drupal\Component\Utility\XSS' not found
I have no idea what further steps I should take. Is the call incorrect? Searching on it seems to suggest the usage is correct...
Having a quick look on the docs the class name is Xss:
namespace Drupal\Component\Utility;
class Xss {}
So call it like:
\Drupal\Component\Utility\Xss
Related
So I've been in the process of updating someones old CI 1 to CI 3 code. In process. In particular, the URI class extension is not working. I've read the CI documentation switched to __construct() and moved it to the application/core directory. I've checked SO and all cases are correct, but I still get the following error:
Call to undefined method MY_URI::last()
My code below
class MY_URI extends CI_URI {
function __construct()
{
parent::__construct();
}
function last()
{
return $this->segment(count($this->segments));
}
}
Thoughts as to why this may be happening with the switch? Checking StackOverflow it said chek your config settings by the config has the correct
$config['subclass_prefix'] = 'MY_';
I'm calling it with:
$lastURI = $this->uri->last();
Update: I've also tried the
exit('MY_URI.php loaded');
trick at the top which seems to work, but it still throws the error when I remark it out and never loads the extension.
Place your MY_URI.php file inside the application/core/MY_URI.php & update the function like following.
public function last(){
return $this->segment($this->total_segments());
}
call it like below
$last = $this->uri->last();
I am using Drupal 7 and I am using my custom module's .info file along with the "files[] = ...." directive to allow autoloading of my classes when required. Each class only contains static public functions and everything is working nicely when these functions are called from a function of the general scope.
Working example:
If I go to /devel/php and call the function ClassTwo::getValue() everything is executed without error.
Example with error:
If I call the function ClassTwo::getValue() from within another autoloaded function ClassOne::generate() an exception is thrown that the class ClassTwo() doesn't exist. The error is solved if I specifically include the file containing the class ClassTwo.
I don't believe it has something to do with Drupal's autoloading mechanism though. Is my scenario allowed in PHP 5.3?
For better clarification I have added below example code. That's the nearest I can provide to working code.
mymodule/mymodule.info
name = "MyModule"
core = "7.x"
files[] = "includes/plugins/ClassOne.inc"
files[] = "includes/plugins/ClassTwo.inc"
mymodule/mymodule.module
function mymodule_page_callback() {
return ClassOne::generate();
}
mymodule/includes/plugins/ClassOne.inc
Class ClassOne {
static public function generate() {
$value = ClassTwo::getValue();
// Process $value
return $value;
}
}
mymodule/includes/plugins/ClassTwo.inc
Class ClassTwo {
static public function getValue() {
// Somehow retrieve the value.
return $value;
}
}
UPDATE: Using Drupal's xautoload module with PSR-4 everything works incredibly fine and I have turned to this method as a solution. If anyone has an answer though, other than PSR-0/PSR-4, I would like to know it.
I am using OpenCart and installed the module sizes.xml, it edits the catalog/product/model/catalog/product.php file
It adds a class method;
I have an issue with some code where in a function I have:
class ModelCatalogProduct extends Model {
public function getSizes($product_id) {
...
} // end function
} // end class
We always get an error saying that we cannot redeclare getSizes method.
I want to either force redeclaration, or alternatively check whether the function exists before calling the method but I cannot use function_exists in a class.
ie, this seems illegal;
class ModelCatalogProduct extends Model {
// This throws up an error
if (function_exists('getSizes')==FALSE) {
public function getSizes($product_id) {
} // end function
}// end if
} // end class
It will throw up an error issue. But I need the function in there.
I would like to use function_exists in this class or alternatively force the method to be redeclared.
How would I make function_exists work inside a class?
You can't have executable code in a class that is outside of a method, so there's no way you can do what you're asking, as your if() condition would need to be in the class body.
So despite what others are saying, method_exists() is not a suitable answer to this question.
If you're getting an error stating that the method is already declared, then there are a few possible reasons for this:
It is actually already declared elsewhere in the same class. In which case, of course you can't redeclare it. But since the code for the class ought to all be in a single file, then it should be fairly easy to see that and avoid doing it.
It's declared in the parent class (ie in your case Model or one of its parents).
Under normal circumstances, you should be able to redeclare a method that is already declared in a parent class; your method would override the method of the same name from the parent class. So for most cases, your whole question is entirely unnecessary.
But you say you're getting errors, so clearly something is going wrong It would help if you'd told us the exact error message, but there are two reasons I can think of this might not work:
If the method in the parent class is declared as Final, then it means the author of the parent class explicitly doesn't want it to be overridden. This means that you cannot have your own method of the same name.
If the method in the parent class has a different signature - eg it's private in the parent but public in your class, or static in one but not in the other, then you will get errors complaining about that. In this case, you'll need to make sure that the methods have the same signature, or else give your method a different name.
Hope that helps.
The answer of #Spudley is correctly chosen as the right answer.
Just more explanation on the first case mentioned in the answer: if you wand to declare the function inside of a method of a class, you should also consider the namespace of the class:
class MyClass
{
function myFunction()
{
//here, check if the function is defined in the root namespace globally
// or in the current namespace:
if(!function_exists('someFunction')
&& !function_exists(__NAMESPACE__ . '\someFunction'))
{
function someFunction()
{
}
}
//....
someFunction();
}
}
If you don't check the second condition of if, calling myFunction() more than once would throw exception
I'm having trouble with accessing the session in an external .php script located in webroot.
Thought I'd write a function getSession() in one of my controllers and try to call it in the .php file.
So in steps:
I have file.php
In a controller I have a function getSession().
How to call the controllers function in the file.php?
Thank you.
EDIT
Meanwhile I fixed my bug, but still am curious how this is done and want other stack users to find a good answer to this so:
Its exactly like this:
In UsersController I have a function:
public function getSession() {
return $_SESSION['Auth']['User']['user_id'];
}
That I want to let's say print (for example) like this: print_r(Users.getSession) in the file test.php located in webroot/uploadify/test.php.
This file is not a class, but if it is required, then it shall be :)
#CaboOne: Maybe your answer was correct, I just wasnt sure what code to call (and enter) where :)
Supposed I have the following php file in webroot folder:
<?php
class TestingClass {
function getName(){
return "Test";
}
}
?>
I would do the following:
// This would bring you to your /webroot folder
include $_SERVER['DOCUMENT_ROOT'].'/another_file.php';
// Initializing the class
$example = new TestingClass;
// Call a function from the initialized class
$a_value = $example->getName();
// If you want to use $a_value in the view, you can then set
$this->set('a_value', $a_value);
i m learning OOPS with JOOMLA... here sometimes i found difficulties to find the method used in some class...
is there any way to find that this function is declared on this class or useful information about that function??
for exmaple
class testModeltest extends JModel
{
function modifyCategory($data = array())
{
$image = $this->imageResize($value);
.......
}
}
now i want to know where the imageResize() function declared/defined first time...means class and file name where this function born
i used magic constact __METHOD__ this retrive useful information inside class . i need such type of function where i just put method name & i get the complete information of that function
i want a below kind of facility( i m sure there are some function in php to get the information about class but don't know )
functionInfo($methodname) // here i just put the function name
which return
Function Name:imageResize
Main class : imageclass
File name where it has been declared : /foldername/filename.php
currenty using(called) in : thisclass::this function
If you are looking for the place where a method was first defined, that should be possible using get_parent_class() - here is a snippet that walks through each class definition - and doing a method_exists() on each class found that way.
However, this will not show where the method has been subsequently overriden, so it may be of limited use to you - in that case, something like Reflection is probably indeed the only way.