"Publish" PHP functions [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am still pretty new to PHP but I was reading through WordPress pages to get ideas for my own site and saw <?php get_header(); ?> and I remembered that I have always wondered how the developers of WordPress made these "public functions"
There is probably a real name for them and they probably aren't even functions but I have been wondering if I could create something like this for my own website. I know you can use <?php include 'header.php'; ?> but what if for some reason I want to be really lazy or have long file names/paths? Could I make it simple and quick?

For your own website, you can create functions that wrap long file names for includes. This approach can work whether you are encapsulating the function in a class, or if you are leaving it in the global namespace (as others have suggested...that's another topic for debate).
If writing your function in the global namespace, you may want to consider wrapping your function declaration in an IF statement that checks to see if the function has already been declared. This might allow other developers to replace your method with their own (for example if you are including a view or something visual they want to replace).

Related

PHP - All classes and function in a single page [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
On php, i want to put all classes and functions on a single page. This page will be called throughout my project. is it recommendable or a bad practice?
It is bad practice. It is called a big ball of mud anti pattern. Try to keep everything modularized. It will be more maintainable and readable. You will thank yourself later.
Sure, it's possible. These files are often called "helpers". You could compile all the functions and classes that you need throughout your website in one file (e.g. functions.php). However, you should not forget to properly split your code apart into different files, to prevent creating a big mess. You can then use the functions/classes like so:
require 'functions.php';
$class = new MyClass(); // Use a class from the functions.php
myFunction(); // Use a function from the functions.php
But! Note that this file can now also be accessed via a URL. For example: http://example.com/functions.php. This can, in some cases, cause unwanted behavior. You can restrict the "direct access" to this file using .htaccess rules or simple place this code on top of the helper file:
if(count(get_included_files()) == 1) die();

How can i disable X-cart 5 Flexy template engine and use PHP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Flexy is lack of documentation and maybe because of that I really don't like it at all. I want to use regular PHP in my module, is it possibly?
The best solution would be if I could use PHP just for my module, and do not apply it site-wide.
Thank you.
You can defintely do it, though it is not recommended, by overriding display() method in each of your View classes.
The basic implementation is described in \XLite\View\AView class. You can override it in your View class to print in buffer the output of your custom php script like this:
public function display($template = null)
{
include 'custom_php_template.php';
}
While having 'custom_php_template.php' with similar contents:
<?php
echo 'Hello world';
Please note that this is just the simple example and the real logic can be a lot more complex. Also, by using this approach you may need to create or adapt existing caching solution and other things by yourself.

Is-it worth to split a page into many pages? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've got a question about php and pages :
Is-it worth to split the code into many other files ?
So at the end, the index.php would be composed of :
include 'footer.php';
include 'header.php';
include 'home.php';
include 'load.php';
[...]
Or should I put all the code in an unique page ?
Other question, if I include a php page, could all the vars created in the included page be used in the main page, or should-I use $_SESSION ?
Thank you all for your help !
As for your main question it’s worth. In fact, it will be a pain for you to manage your web page if you don’t follow this method in the long run. I am not a php guy but I can tell you this because this is a universal truth. The idea here is to write once and use anywhere necessary. You don’t want to write the same thing again and again, do you? That’s why we produce function when programming. We define a function and use it as many times we want. Now consider you have suddenly found that you need some modification to do , as you have created a function you can make the change in just one place and it will be reflected everywhere without touching a single line of code outside the function’s code. In actual project change is a constant. That’s why we find many design pattern in the software industry like MVC.
In a web project, mostly (not always) the header, navigation, footer are same across all the pages. Therefore, you should consider making different file for these stuff and you should always put your content in your index.php file which are unique for index page.

Is there a way to define global or standard function in php that can be called from anywhere without including the file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Very frequently, I need to use codes like this to see output clearly:
$a=print_r($var,true);
echo "<pre>$a</pre>";
How can convert this piece of code to a global function (for example:
echopre) in Php so that I can use echopre($data) anywhere in php
program (in my server)?
Thanks,
Don't think you can have PHP load functions everywhere, per default, without inclusion.
However, you can definitely do it with classes (which, other than having to either create an object or directly access with YourClass::echopre() will work out the same).
Check the php config include_path option.
If server administrator is you, you can write custom php extension and add some functions in php. For example I wrote ar_view function, which is alias of print_r with html formatting: https://github.com/akalongman/ar_view

How to separate this PHP and HTML source [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a change_username.php which makes it possible for users to change their username. If they changed their username a page like "Your new username ist xxx" or "xxx is already used" will be displayed.
I don't want to use an echo and put all the HTML stuff in there.
My first idea would be to create another 2 PHP's which then get the username from the change_username.php and display a proper result with HTML.
Are there any alternatives / better ways to do that? Creating files for every result sucks as much as putting the HTML data in an echo.
If I understood your question correctly you want to include a page with in your PHP: try using this as reference http://www.w3schools.com/php/php_includes.asp
You can include the page using
include 'filename';
or
<?php include 'menu.php'; ?>
I hope this helps you
I would do it a little bit differently, I think the problem is with managing the code where large page is getting messy. What I do in these cases I just use functions with parameters. So I create a lib.php file, where I store function with HTML blocks of code being echoed. Then when I need I call these functions in the necessary files, just requiring lib.php at the top of my page.

Categories