How to call static func by passing $_GET params? - php

i have got a static function> which is called
regenerateThumbnailsCron()
And I would like to execute this function by GET params, for example>
if($_GET["pass"]=="password")
self::regenerateThumbnailsCron();
But if I tryied to call this function in constructor>
class AdminImages extends AdminTab
...
public function __construct()
{
if($_GET["pass"]=="password")
self::regenerateThumbnailsCron();
}
I cannot execute this function.
Is any way, how to call this function before __construct to correctly execute?
Thanks very much for any advice.
EDIT>
I tried also with public function>
<?php
include 'AdminImages.php';
$images = new AdminImages();
$images->regenerateThumbnailsCron();
?>
But i got error>
Fatal error: Class 'AdminTab' not found

You need to do a include 'AdminTab.php'; as well, since your class extends that

Not completely sure I understand your question, are you saying that you have static class "B" which extends class "A", "A" having your regenerateThumbnailsCron() method which you want to call before anything else?
If so then try this:
<?php
class A {
private function regenerate() {
.... do something ....
}
}
class B extends A {
function __construct() {
if ($_GET["pass"] == "password") {
parent::regenerate();
}
}
function regenerateThunbnailsCron() {
.... do somethinig ....
}
}
$images = new B();
$images->regenerateThumbnailsCron();
?>
This way, your parent's "regenerate()" function would get called during the constructor. You can switch this around to be a static class if you want, which if your goal is to compartmentalise any variables and functions away from the global scope would be a better way.

Related

PHP Access a method in global

I'm triyng to convert a normal script in PHP to OO. I'me having some troubles doing that for some things. one of that is call a function (or method) in a variable to use globally.
The initial code is the following:
$myVariable = myFunction();
function myFunction(){
// some code in here
}
I've triyed to do something like following but it doesen't work
class MyClass{
$myVariable = $this->myFunction();
function myFunction(){
// some code in here
}
}
Can someone help me?
You need to call the function in a method (most likely the constructor). if you want to access to the property all over the class definition do this:
// Imagine you have a global method like this
MyFunction() {
}
class MyClass {
// define a property that store the result of calling MyFunction()
protected $somethingThatYouNeed;
// constructor calls when you instantiate your class
public function __construct() {
$this->somethingThatYouNeed = myFunction();
}
public function otherMethod() {
// you can access your variable here by calling $this->somethingThatYouNeed
}
}

PHP constructor not called

I have problem, can i call constructor without create 'new class()' ? Or you maybe have another way for this :
<?php
class a
{
public static $hello;
public function say()
{
return self::$hello;
}
}
class b extends a
{
public function __construct()
{
self::$hello = 'hello world';
}
}
echo b::say();
?>
I have try with :
$b = new b();
echo $b->say();
And it's work. But i want to use b::say();
Can help me?
Thank you!!
Check out this. Is this good for you?
<?php
class a {
public static $hello;
public static function say() {
return self::$hello;
}
}
class b extends a {
public function __construct() {
self::$hello = 'hello world';
}
public static function factory() {
return new b();
}
}
echo b::factory()->say();
?>
Actually I couldn't find a way to do this without calling constructor. This is how the workaround looks like. factory is just a name. you can rename it.
calling class method (with constructors) without object instantiation in php
You have asked: "can i call constructor without create 'new class()' ?"The answer: No.
... Classes which have a constructor method call this method on each
newly-created object
You have requested "But i want to use b::say();"
b::say(); - is call of static method.You can't override non-static parent method to static. But you can restructure your base class class a to make say() method static.
<?php
class a
{
public static $hello;
public static function say()
{
return self::$hello;
}
}
class b extends a
{
public function __construct()
{
self::$hello = 'hello world';
}
}
The thing that you were missing was you needed to add your content to your new class method. - Just call it like so:
$b = new b('Some words');
echo $b->say();
When calling a new class and using a constructor - You will want to add the content in the paramaters for the new class you are making.
It acts as if you are calling the __construct function. - Calling new class($a) will call the __construct($a) function once making the object.
Hope that this helped a bit :)
Yes it is possible just make the say() function static like this :
public static function say()
{
return self::$hello;
}
Declaring class methods as static makes them accessible without needing an instantiation of the class.
This example looks to me like late static binding. So try changing that return self::$hello; into return static::$hello;

Using class inside a function

I'm trying to use my class inside a function called inside()
I have something like:
class MyClass{
public function test(){ echo "test ok"; }
}
$mc = new MyClass();
function inside(){
global $mc;
$mc->test();
}
But this doesn't work :(
I get:
Fatal error: Call to a member function test() on a non-object in ...
One solution would be to pass the class $mc as an argument to the function inside() but I want something else :/
Any idea ?
Ty guys
Since the inside function depends on the MyClass class, it should be passed in as a dependecy injection.
$mc = new MyClass;
function inside(MyClass $mc){
$mc->test();
}
It looks like you need a refresher of the (Gang of Fours) injection design pattern, http://martinfowler.com/articles/injection.html
If all you want is to access a function residing inside a class you can define it as static
class MyClass {
static function test() {
echo "test ok";
}
}
Then you don't have to use a global:
function inside() {
MyClass::test();
}
You need to define test as public function test(){....

Calling function from another php file

I have 2 file
file1.php
<?php
Class A
{
public static function _test
{
}
}
function get_sql($id)
{
}
function get_data($ids)
{
}
?>
In file2.php I've written
require_once('file1.php');
$a = get_sql($id);
Why I cannot call the function and get my result??
try this in file1.php
<?php
Class A {
public static function _test {
}
function get_sql($id) {
echo $id;
}
function get_data($ids) {
}
}
?>
In file2.php first require the file and then code this
require_once('file1.php');
$a = new A();
$a->get_sql($id);
OR send static value in function
$a->get_sql(5);
This is your first mistake in your code
public static function _test{
}
} //this bracket is related to the class
Well for one thing you are not returning anything from the get_sql($id) function.
Assuming you are returning something in your original code; I hope you are aware that the function is not part of the class (its defined outside the scope of the class). But for educational purposes you would call a static method within a class by doing:
$a = A::get_sql($id);
This would also mean the defining the function in the following manner:
Class A{
public static function get_sql($id){
echo $id;
}
}
it is the question if you want to have functions get_sql() and get_data() as a methods inside the A class:
If yes the code from user2727841 will work after you add the round brackets to the function public static function _test:
public static function _test()
{
}
Your code will work too after you add the same brackets to the same function but your function get_sql() and get_data() are outside the class A.
EDIT
I thought that these function are outside the class A.
Please, add the round brackets to the public static function _test in the class A - it is syntax error - than I hope it will work.

PHP: Calling a private method from within a class dying badly

So this might sound a little convoluted. Fingers crossed I come across clearly.
I'm working in an MVC framework in PHP.
I load a controller /report/index which calls to a helper
<? class ReportController extends Controller {
public function index() {
$foo = MainReport::get_data($_REQUEST);
}
}
?>
Inside the helper
<? class MainReport extends foo {
public function get_data($_REQUEST) {
// do stuff
return $stuff_done;
}
}
?>
It I run it like ^this all's well and good. Unfortunately, I want to run it like this:
<? class MainReport extends foo {
private function do_stuff() {
// do even better stuff here!
return $better_stuff;
}
public function get_data($_REQUEST) {
// do stuff
$x = $this->do_stuff();
}
}
?>
Unfortunately... when I try and call a private function from within a class that I've called from elsewhere... (whew, that's a mouthful) ... everything dies. Dies so very very badly that I don't even get an error.
It seems obvious to me that I'm having an incredibly dorky sort of syntax issue of some sort... but how do I correctly access private functions from within a class?
Maybe something like:
self::do_stuff();
What about declaring and accessing private class variables?
private $bar = array();
Any help would be welcome.
You are calling your function from a static context,
MainReport::get_data($_REQUEST)
therefore $this does not exist while inside that function.
If you want to call another class function while inside a static context, you have to also call it statically.
i.e.
public function get_data($_REQUEST) {
// do stuff
$x = MainReport::do_stuff();
}
Alternatively, you can create an instance of your class in the original call and use the instance:
$myMainReport = new MainReport();
$myMainReport->get_data($_REQUEST);
Then your class code will work as expected
I've just found that self:: does work as well
if I want to have private class variables, I can declare and access them using
private static $foo
and
self::$foo = "foo";
additionally a private function can be accessed with
self::function_foo();

Categories