I'm working on WordPress plugin where I have 2 php files.
class.php has code as:
class Plugin_Name {
public function say_hello() {
echo "Hello";
}
}
Now I want to call this say_hello() function from another welcome.php file.
I tried
$hello_function = new Plugin_Name();
$hello_function->say_hello();
But it doesn't work. Is there any way to call public function from another php ?
You need to include the first function in the other file, that way the other file knows the code is there. At the top of welcome.php add
require_once('/path/to/class.php');
$hello_function = new Plugin_Name();
$hello_function->say_hello();
Related
I am building a website using php and i have used many functions through different pages.
I was thinking of collecting the functions to a single functions.php file and include it on the page i want to use the function but that would also include other functions which are useless on that page, so is there a way to include only the nessesary functions from the functions.php file instead of including them all?
Thanks,
You may not have the Luxury to pick and choose the functions to load from a File. However, you may try putting all those Functions of yours into a Trait. Then use them within a Trivial Class wherein it would be imported. Here's an Example:
<?php
trait PreferredFunctions {
public function doActionOne($param1){
// CODE! CODE!! CODE!!!
return null;
}
public function doActionTwo($param2){
// CODE! CODE!! CODE!!!
return null;
}
public function doActionThree($param3){
// CODE! CODE!! CODE!!!
return null;
}
}
class Trivial{
use PreferredFunctions;
public function __construct() {
}
}
$trivial = new Trivial();
$result1 = $trivial->doActionOne(1);
$result2 = $trivial->doActionTwo(2);
$result3 = $trivial->doActionThree(3);
I am having a really weird error, I have this template Engine (which I think is working correctly since it shows most files correctly):
<?php
class Template
{
protected $template;
protected $vars = array();
public function __construct($template)
{
$this->template=$template;
}
public function __get($key)
{
// TODO: Implement __get() method.
return $this->vars[$key];
}
public function __set($key, $value)
{
// TODO: Implement __set() method.
$this->vars[$key]=$value;
}
public function __toString()
{
extract($this->vars);
chdir(dirname($this->template));
ob_start();
include basename($this->template);
//echo $this->template;
return ob_get_clean();
}
}
Then I have this file named topic.php with the following code in it:
<?php
require ('core/init.php');
$template = new Template("templates/topic.php");
echo $template;
?>
And my template "templates/topic.php" looks like this:
<?php
echo "Testo";
And finally I have this other file "templates/test.php" which looks like this:
<?php
echo "Testing";
If in line two of topic.php I change $template = new Template("templates/topic.php"); to $template = new Template("templates/test.php");
It just prints correctly, "Testing" shows in the webpage but if I want to show "templates/topic.php" it doesn't show anything in the webpage. Also if I refactor "templates/topic.php" to "templates/topici.php" for example, it works fine.
This is not a project I need to deliver and the workaround is really simple... is not to use the file name topic.php in the template directory, but I want to understand what is happening.
Note: Also, I have other files with a similar structure where for example the file is named "create.php" and the file it is supposed to show is "templates/create.php" and they all work fine.
Thanks for your help.
I am using the Slim framework for the development of a web-app.
However, I came across some issues which I can not solve.
I want organize my code in classes and call certain methods from the classes.
i have an index.php file in which the following function exists:
$app->post('/', function () use ($app) {
// some code here
//a variable $result I want to get the result from a method of the class Generate_num
$result = (here I want it to take as a result the function "generate" from a .class.php file which I have stored in a special folder "classes"
//another code
});
my class code looks like this
class Generate_num
{
public static function generate()
{
//some code
}
}
Any suggestions ? Thank you !
Given this class:
class Generate_num
{
public static function generate()
{
//some code that creates $number
return $number;
}
}
You call it like this:
$number = Generate_num::generate();
I have a view class with a function to build the view
class view {
//...
public function build() {
$view = $this;
$data = $this->resources->data;
function s($value) {
return \classes\tools\html\html::specialChars($value);
}
require $this->viewFile;
}
//...
}
And a view some view files
<?php var_dump($view);
// works fine, variables passed ok ?>
<?= s($data->someUnsafeString) ?>
<?php //Fatal error: Call to undefined function s() ?>
I could define the function s In each view file but I really dont want to have to do that.
I could pass the function as a variable $s=function(){..} but I'd prefer not too
I could call the static function in the view directly but even that is more long winded than I'd like:
<?= html::s($data->someUnsafeString) ?>
Is this possible? or any other suggestions?
If your project has a Front Controller (one file - entry point),
then you can to declare your function in common namespace in this or required(#require) by this file.
Common namespace is code without namespace or
namespace {
function s() {
}
}
You may be misunderstanding what you're doing there. Functions aren't scoped in PHP. Your function s(..) inside view::build is merely declaring a regular global function. Doing that inside functions isn't anything special. You can simply declare functions conditionally in PHP, e.g.:
if (!function_exists('s')) {
function s(..) ..
}
So just put that function declaration into a separate file and include it as needed; no need to define it again and again separately in each file.
I'm trying to create simple function that can be executed on any page.
function something() {
$string = 'Hello World';
return $string;
}
Let's say I'm in the category page, I would just call $a = something(); and it would return my value
Platform : OpenCart
P.S. I'm still studying MVC architecture
Since you are wanting to understand and learn about the MVC system, the correct way to go about this would be to create your own helper file and put it in /system/helper/ and then add the helper to system/startup.php. Take a look at how the json.php / utf8.php are done in these as a guide
Create a new file to system/library/yourclassname.php with same code.
Also please add a class name to your function as below:
class yourclassname {
function something() {
$string = 'Hello World';
return $string;
}
}
And add it to your index.php file as below;
$registry->set('yourclassname', new yourclassname($registry));
Finally add it to your startup.php file as below:
require_once(DIR_SYSTEM . 'library/yourclassname.php');
You can use it anywhere with $this->yourclassname->something();
Thats All..
You can create a function in any opencart library (system/library/)
As example in system/library/document.php
function something() {
$string = 'Hello World';
return $string;
}
And use anywhere in openсart as
$something=$this->document->something();
P/s code in header.tpl will not work in ajax or direct request
Matricore's answer worked perfectly for me, but I also had to construct the new class with the config and db available.
Code below:
class yourclassname {
public function __construct($registry) {
$this->config = $registry->get('config');
$this->db = $registry->get('db');
}
}
You can then run queries via $this->db->query("Your query here");