Calling method parent class of extension parent class in SilverStripe - silverstripe

I am working on a SilverStripe project. What I am trying to do now is that I am trying to call the method of parent class of the extension class.
I have a class like this
Order extension GenericOrder {
public function validate()
{
$result = parent::validate();
//Some other code
}
}
Then I created an extension class for it
class OrderExtension extends DataExtension
{
public function validate()
{
//here I want to call the validate method of the GenericOrder class
}
}
As you can see in the comment, I am trying to call the validate method of the GenericOrder class which is the parent of Order class which my extension class, OrderExtension class trying to extend. How can I call it?

$this->owner->validate();

Related

BaseController to handle client factory

I want to build a base controller that I can put some reusable methods so I do not have to put a bunch of repeat code in all my controllers. So I built a BaseController.cs
public class BaseController : Controller
{
public IHttpClientFactory _clientFactory;
public BaseController(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
}
Then in one of my contollers I do public class TokenController : BaseController. But then it wants me to add the following but then it gives me errors
public TokenController(IHttpClientFactory clientFactory)
{
// I guess something goes here
}
But then VS Code tells me
There is no argument given that corresponds to the required formal parameter 'clientFactory' of 'BaseController.BaseController(IHttpClientFactory)' (CS7036)
What am I missing here? I been in JS world to long :)
When inheriting classes without default constructors you have to pass parameters to them using the following syntax:
public TokenController(IHttpClientFactory clientFactory) : base (clientFactory)
{
/* other initializations */
}
So add the following expression: : base (clientFactory)
See more information here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors

class from the attached library is not activated

I installed the Nuget package. hooked it through the "using" . The package classes work in another solution, and in my project they do not work
screenshots:
neolux
I think there are two separate issues here. I had an issue similar to #Leo in that the TestForNet() method is part of the NeoDB class.
Assuming that the method exists on the NeoRPC class for you, then #Leo is also spot on in suggesting that you can't create a variable (i.e. var api = NeoRPC.TestForNet()) within a class declaration as you have it in your screenshot.
If you'd like to set api when the class is created, you can create the variable at the class level and assign it in the constructor. I'm going to rely on NeoDB instead of NeoRTC in this example:
public class HomeController : Controller
{
private readonly NeoDB _api;
public HomeController()
{
_api = NeoDB.ForTestNet();
}
public IActionResult Index()
{
// _api.QueryRPC();
}
}
class from the attached library is not activated
You should put the code into the method rather than under the class directly, like:
public class HomeController : Controller
{
public ActionResult Index()
{
var api = NeoRPC.ForTestNet();
Hope this helps.

Use of an overloaded global class in a Symfony controller

I try to include the following code in a Symfony controller file:
namespace {
class LocalSoapClient extends \SoapClient {
function __doRequest($request, $location, $action, $version) {
//...............
}
}
}
namespace .....\Controller {
//.......
}
In the controller class, I try to use the overloaded class:
$service = new \LocalSoapClient($wsdl);
With the \, the class is supposed to be in the global namespace. But I get the error:
Class 1\LocalSoapClient does not exist in .......\FrontBundle/Controller/
Why?
If I use a single namespace instruction, and I overload the class before the Controller class, I get the same kind of error.
What can I do to be able to use my overloaded class?
Thanks in advance
There are 2 DON’Ts that you’re violating:
DON’T put more than one class into one file.
DON’T mix namespaces. Reason is, Symfony uses PSR-0/-4 autoloading and what you’re trying to do cannot work.
The solution is to simply create separate files with proper namespaces.
Put the following into src/Your/SomethingBundle/Soap (or wherever you like the file to live):
<?php
namespace Your\SomethingBundle\Soap;
class LocalSoapClient extends \SoapClient
{
// …
}
And your controller should just look like this:
<?php
namespace Your\SomethingBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Your\SomethingBundle\Soap\LocalSoapClient;
class MyController extends Controller
{
public function myAction()
{
$soapClient = new LocalSoapClient();
// …
}
}

Add custom property to all symfony2 controllers

is there any way to preprocess controller data somehow. I'm going to take param from session, validate it and assign it as controller property and use it as $this->myVar inside actions of some controller or all of them if possible. Using controller's constructor gives me nothing, I couldn't access request and session data. Thanks!
UPD:
Thanks, jkucharovic, very good solution.
Also there is a bit dirtier solution, without injecting: setContainer() method, which has been called straight after $controller = new Controller();
use Symfony\Component\DependencyInjection\ContainerAwareInterface,
Symfony\Component\DependencyInjection\ContainerInterface;
class AppServiceController extends Controller {
private $my_property;
/**
* Used as constructor
*/
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer($container);
$this->my_property = 'foo';
// your controller code
}
}
I'm not sure what you wan't to do is very usefull. A Controller instance will be created each time the controller is called. The session and request will be different each time you call the controller.
I think you should create a BaseController class extending Controller class with a shortcut method to access your MyVar value in session.
class BaseController extends Controller
{
public function getMyVar()
{
return $this->get('session')->get('MyVarSessionKey');
}
}
All your other Controller will extend from this BaseController.
To get the request, just use the shortcut method provided by Controller class, Controller::getRequest().
If you want to use services in __construct method, you have to inject that services first. Then you can use them before any other methods. For example:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session;
public function __construct(Request $request, Session $session)
{
…
}

PHP: Class extends problem "Call to private method ... from context ..."

I have 3 classes in WordPress (the question itself is unrelated to it):
class WP_Widget
class Theme_Widget extends WP_Widget
class Specific_Widget extends Theme_Widget
Essentially Theme_Widget contains some extension functions to the basic WP_Widget.
Inside Specific_Widget I call one of Theme_Widget's methods:
class Specific_Widget {
function __construct() {
$this->some_method_that_belongs_to_Theme_Widget();
}
}
When I instantiate Specific_Widget, PHP throws a fatal error as follows:
Fatal error: Call to private method Theme_Widget::some_method_that_belongs_to_Theme_Widget() from context 'Specific_Widget' in ...
Do you have an idea as to how I can resolve this? This is the first time I've received this error from PHP. Could it be derive from WordPress itself?
You must declare your method protected, rather than private, if you wish child classes to be able to use it.
use protected function if you would to access a child functions from your extended class's without passing the protected function in URLs
for example
protected function somemethod() { // your code goes here }

Resources