Cannot require two classes in php [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 2 years ago.
Improve this question
I am currently working on a little website and i am trying to require two classes. When I only require one class, everything works fine but when I require two classes my page is not working any more.
This code works:
<?php
require '../assets/php/mapManager.php';
$mapManager = new mapManager();
?>
This code works:
<?php
require '../assets/php/accountManager.php';
$accountManager = new accountManager();
?>
This code works not:
<?php
require '../assets/php/mapManager.php';
require '../assets/php/accountManager.php';
$mapManager = new mapManager();
$accountManager = new accountManager();
?>

My hypothesis is that each of those class files includes or requires the same file containing a parent Manager class or some other common dependency, and you're getting a fatal error when you try to redeclare the class in that file. You can fix this by using require_once instead of require, or ideally by implementing autoloading.
Normally I wouldn't post a "guess" answer, but based on the context and naming and the way the error is occurring I think this is extremely likely, and if it doesn't happen to be the problem in your case, it definitely will be for someone else with the same question.

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();

When is require ever preferred over require_once in PHP? [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
The way I see it, if a file has been included once then any object, function or member in it is defined.
require_once checks if the file is included and if so, doesn't include it again. But when would the event ever arise that someone would go 'this file has already been included so I can use the class inside it, but I'd better include it again because this script needs it'?
Am I missing something?
I use include_once / require_once for classes files, and include / require for html code (let's say a form for example). You shouldn't redeclare the class, but you could insert html code multiple times in your code.
In ye olde days before proper classes, I would sometimes use require inside a for or while loop when doing large imports. Along the same lines, template engines use it for repeated includes of the same file that should fail if the file is missing.

Why does the function require_once prevent my code from working [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
At the top of my code I have the following
require_once 'PSBE_LOGIN';
where PSBE_LOGIN has all the information to access my database(I'm using a PDO connection). However, my code does not work but when I take it out, my code works perfectly. Any thoughts on why this is? I need the file there so I can collect information from my database.
Require (as opposed to include) will halt the execution of a script on failure. For example, if the specified file is not found.
Thus your code can be fixed by ensuring the file exists and in the correct directory.
Hi try this ( no quotes )
require_once PSBE_LOGIN;

What mean Models in PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to create a RESTful API with PHP and tutorial I found is: http://coreymaynard.com/blog/creating-a-restful-api-with-php/ and everything worked pretty well only in a part of the tutorial did not understand something.
Near the end of the tutorial, when you create a script to run the API does the following:
$Apikey = new Models\APIkey();
$User = new Models\User();
I do not understand what the "new Models\Whatever();" someone could advise me a bit about it and how it works?
In advance I thank you very much.
Aside from the badly worded question, it's not a bad question so I'm not sure what all the abuse in the comments are about.
That syntax is just instantiating a class as normal - the classes in this case being APIKey and User. The only difference is that these classes are in a namespace (Models). You must refer to your class within the namespace it belongs to.
Generally when writing PHP without namespaces, everything exists in the base namespace, so you can refer to classes within the current namespace by just their names. If you have namespace Models written at the top of your page, you can refer to those two classes in your example as just APIKey or User.
If you are within a namespace, and want to instantiate a class that is in the base namespace, you can do this:
namespace Models
$api = new \Api();
Similar to file systems, the preceding \ just refers back to the base namespace.

"Publish" PHP functions [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
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).

Categories