To be specific, I don't really know what I am doing, but I'm trying to reference $this from another file.
I'm working with Wordpress(subscribe2 plugin) and trying to get the function of subscribing to specific categories from the plugin and putting it into my own signup form.
The ability to do this is already in the settings for the plugin, so I'd like to put them in my own form, but they look like this:
$this->get_usermeta_keyname('s2_subscribed')
so my question: How do I reference "$this" in my own file? I have been googling it all morning, but all of my results see "$this" as "this" and don't show me what I want.
$this refers to the current class context. So if you have a class named MyClass, within that class you can access functions and elements using $this. Outside of that class, $this has no context and won't work, but if you have instantiated the class and if the functions or variables are public, then you can access them via a reference to the class.
Example:
<?php
Class MyClass {
$class_var = "Class Var Value";
function class_func_1() {
print $this->class_var;
$this->class_func_2();
}
function class_func_2() {
print "Class Func 2";
}
}
?>
So the dummy class above uses $this to reference its own elements. But outside the class you cannot reference them with this. But if we instantiate an instance of the class, we can access them by the reference to the instance of the class:
<?php
print $this->class_var; // Fails miserably
$this->class_func_1(); // Also fails
$class_instance = new MyClass();
print $class_instance->class_var; // Var access works
$class_instance->class_func_1(); // Call method works
?>
Since this is a wordpress function, I'm curious if you are trying to access something in a plugin (which might use a class) or possibly something in a purchased theme. Either way, there should be a reference to the instance of the class somewhere in the file. Once you have this variable, then you can access the actions and contents of the class.
With more information, I think we can probably provide better direction...
Update:
Ok, it appears you are using the subscribe2 plugin. At the bottom of the plugin file (subscribe2.php) is this line:
$mysubscribe2 = new subscribe2();
What this is doing is creating a global variable that points to an instance of the class. So instead of using $this->method_name() you could use $mysubscribe2->method_name().
However, looking at the file you posted - it almost looks like you grabbed a portion of the plugin code and just put it into a template. I'm not sure what you are trying to accomplish but from here it appears you are heading down a rough path...
Related
I'm currently using a crud application to create a blog style site.
The blog object requires an upload of a file, which I store in the database as a string. However, when I try to edit an existing Blog, I get this error.
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.
I'm trying to figure out how to do this. A google search did point me to the page where view transformers a re explained. But the example there shows how to transform a string to an Entity (and Issue). However, I'm not sure if this is exactly the way I should handle this, since the object I am trying to transform a string into is a class Symfony\Component\HttpFoundation\File\File. I was just wondering of anyone know how I could go about doing this. I'm under the impression that I would have to get a actual file type of the class Symfony\Component\HttpFoundation\File\File and that is what I am stumped on. How does one do this?
Ok. so it did take me a while to figure this out, but it was my first time. Essentially what I did was use the CallBackTranformer() function in the BlogType.php class and transformed the object, in this case the File to a null object when rendering it in the form, and then just returned it in the other direction.
Here is the code, in case anyone wants to see it.
$builder->get('imageUrl')->addModelTransformer(new CallBackTransformer(
function($imageUrl) {
return null;
},
function($imageUrl) {
return $imageUrl;
}
));
In your updateAction or editAction in the begining of method add somethink like this:
public function updateAction(Request $request ,$posts) {
$posts->setFile(
new File($this->getParameter('images_directory').'/'.$posts->getFile()
));
Just like in the symfony site:
https://symfony.com/doc/3.2/controller/upload_file.html
use Symfony\Component\HttpFoundation\File\File;
// ...
$product->setBrochure(
new File($this->getParameter('brochures_directory').'/'.$product->getBrochure())
);
I am trying to implemt the following instruction, as to have Enum type somehow
Shame on me, but I have not an idea on how/where I go to "register [the defined] type with Type::addType('<enummyfield>', 'MyProject\DBAL\<EnumMyfield>Type')".
EDIT Answer 1 helps. It seems I need too:
to move definition of EnumMyfield to directory MyBundle\Doctrine\DBAL\Types\Type (with appropriate use declarations)
to update app\config\config.yml with lines
types:
<myfield>: <mybundle>\Doctrine\DBAL\Types\Type\<EnumMyfield>Type
since I wish to have a Select on the Form side, to define:
->add('MyField','choice', array('label'=>'Select please', 'choices'=>array('A'=>'A','B'=>'B')), within my MyentityType\buildForm().
With respect to the last point, if I just use choices'=>array('A','B'), values for the select options are rendered as numbers (0,1), and I run into an error (I am not sure why)
your comments/advises are welcome
Just a recap, useful for others (maybe); I will highlight were you're blocked
Create a directory Doctrine\DBAL\Types
Define your new DBAL type there like shown into your link
Register it into your bundle main file (*) <--- this is what you're missing
Use it into entity definition
(*) You have a file inside your bundle named YourBundleNameBundle.php this file is used to register the bundle. If you want to register your custom type also, put inside this bundle the string Type::addType('enum', 'MyProject\DBAL\EnumType')".
So, something like
public function boot()
{
if (false === Type:hasType('enum')) {
$em = $this->container->get('doctrine.orm.entity_manager');
Type::addType('enum', 'Path\To\Bundle\Doctrine\DBAL\Types\EnumType');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum','enum');
}
}
Don't forget the use statement
use Doctrine\DBAL\Types\Type;
at the top of the file
Can i create helper function like codeigniter in symfony2?
I want one function which should print array inside pre tag like
public function print_in_pre_tag($array) {
echo "<pre>";
print_r($array);
echo "</pre>";
}
I often print array like in that format to check the values.
Please suggest some solution and let me know where can i keep the function?
Edit 1: If i call like print_in_pre_tag($array); inside any controller
above function should invoke.
You should create a service (helper in codeIgniter) for that.
Create a folder called Services in your bundle. Create a file in that folder called "PrintManager.php" (or however you want to call it - but make sure the first is capital)
Then inside PrintManager.php you put:
namespace Company\MyBundle\Services;
class PrintManager {
public function print_in_pre_tag($array) {
echo "<pre>";
print_r($array);
echo "</pre>";
} }
Then in your services.yml you set the file:
parameters:
print_manager.class: Company\MyBundle\Services\PrintManager (notice, no .php extension)
services:
print_manager:
class: "%print_manager.class%"
And then in your controller you can just call it like this:
$printManager = $this->get('print_manager');
$printManager->print_in_pre_tag($array);
Btw the best thing you can do is let your service handle the functional part and let it return the result to your controller and from there you work with the results.
like: $text = $printManager->print_in_pre_tag($array);
Actually, I do not understand why people recommend Services.
Services have to contains business logic.
For you task use Helpers. They are static, and you do not need instance!
You should use the LadybugBundle which exactly does what you want. It's much more easier to use as you can debug with calls like:
ld($array);
ldd($array);
Moreover, those helpers are available anywhere in your PHP code without requesting or defining a service. Finally it also works to debug in the console, Ajax calls, Twig templates...
Actually, the question is too old to answer, and actually my answer is not about a Helper, it is about the function that you need. You need a pretty var_dump().
From Symfony 2.6, we have the VarDumper Component. You can use it wherever you need, whenever you need, in your php code, of course.
You have to use the function dump(). You can see the dump in the Developer Bar, and also in a redirection page. The output of the dump, it is better formated, because you have arrows to expand all the inners arrays.
Just to let you know guys
Buddypress has a group functionality in which I combined with the plugin BP Group Hierarchy so that I can create an hierarchy of groups based on user role.
However, the plugin used an method as taught by Buddpress in group-extension-api> link.
The group steps are registered using the function bp_register_group_extension and add_action are called. I tried to remove the action by with no success. Because I not really understand how the array works i.e. array( &$extension, \'_register\' ), so I go search out and found this post.
There's a line stating that
The new format for the above object referenced method callbacks are always: class name, followed immediately by the method name, followed by the amount of previously added (classname+methodname). For classes, this allows you to add as many object referenced classes and add methods which don’t override each other.
However I can't seems to be able to remove the action.
I tried to remove the action by putting following lines of code in function.php
function remove_bp_hierarchy(){
if (has_action('bp_actions')) {
echo $extension = new BP_Groups_Hierarchy_Extension;
remove_action('bp_actions', array( &$extension, '_register' ), 999);
} else {
}
add_action('bp_init','remove_bp_hierarchy', 999);
Is it something wrong with my remove_action or I use wrong method? Thanks and regards.
## Update
Found a page in which let we see a list of hooks and also hooked function in the page. I see that there's a function with the name _register which is the function I'm looking for. However, class address always change. I was thinking using the function found to do a preg_match on it and remove it when it found. this is super heavy. So is there other way of removing it? Thanks and Regards.
CodingBabyDotCom -
Long story short: you will have to traverse the $wp_filter array to remove the action.
You need a reference to the SAME instance that was used to create the action in order to remove it with the remove_action function. So the function you posted doesn't work because it is using a new instance.
Unfortunately bp_register_group_extension() creates only a temporary instance, so it can't be referenced by later functions.
The code in your comment will remove ALL actions at level 8, which means all group extensions. To remove only the one you want, iterate over each filter and check its type with:
is_a( $wp_filter['bp_actions'][8][$key], 'BP_Groups_Hierarchy_Extension' )
This is my first time here, but I already found some good answers here, so I'd like to thank everyone.
I'm developping a small Flex application and I want to instantiate every class from a package into an array, so I can parse it afterwards. To clarify, I'm trying to ease a plugin management system for my application, with the old canProcess/doProcess routine :
My plugins are all in ONE package, including an abstract plugin class. First, I create one instance of every classes in this package (that's where I need help) and put them in an array. Then, whenever I need a plugin for an item, I parse every plugin class in my array with the canProcess method (the item is the parameter). If one plugin says yes, then I send the item to the doProcess method and stop parsing the array.
I know I could implement by hand every class in my package, but I'd prefer not bothering to do it.
Has anyone an idea ?
Thx
AS3 reflection doesn't allow you to list all classes in a package. You will have to write the class names to an (xml) file at the server, load it and then use getDefinitionByName to get Class objects from those strings and then instantiate them.
Consider the sample xml file:
<root package="boris.ratak">
<className>Plugin1</className>
<className>Plugin2</className>
<className>Plugin3</className>
</root>
load it with URLLoader and parse it like:
import flash.utils.getDefinitionByName;
var pack:String = String(xml.#package) + ".";
for each(var cl:String in xml.className)
{
var name:String = pack + String(cl.text());
var Type:Class = getDefinitionByName(name) as Class;
pluginArray.push(new Type());
}