I have a class Database in database.php with 145 functions(17,000 lines) and now i've read this is bad practice, so i want sort out the functions correctly into specific classes rather than a "God" Class.
What i want to know is how do i call a function from another class? Below is an example; How do i call function two from within function one?
database.php
require("connect.php");
class Database {
private $connect;
function one() {
//call function two
}
}
forms.php
require("connect.php");
class Forms {
private $connect;
function two() {
//returns forms
}
}
How do i do this?
In the example you gave you would do:
function one() {
$forms = new Forms;
$forms->two();
}
Another option would be
function one() {
Forms::two();
}
And in Forms you would change the method to:
static function two() {
}
Related
I'm new to object-oriented PHP, and still wrapping my head around classes and subclasses.
In this simple example, $this->siteName will be empty inside the instantiated class Two (called from class One's method "build").
Is there any way for an instance of class Two to inherit that property value set by the parent?
I know I could just turn class Two into a method of class One, but I like having separate files for organization.
Is there a better way to do this?
File One
class One {
protected $siteName;
function __construct() {
$this->siteName = 'Example';
}
public function build() {
$two = new Two();
return $two->build();
}
}
File Two
class Two extends One {
public function build() {
return "<h1>$this->siteName</h1>";
}
}
By the sound of it you're trying to solve the wrong problem.
My understanding of your question is that you basically want to have the code for public function build() in another file for organisation reasons.
Here's how:
class One {
private $siteName;
public function __construct() {
$this->siteName = 'Example';
}
use One_build;
}
Assuming you have the proper autoloading setup, or have included the files correctly, you can then have your separate file:
trait One_build {
public function build() {
return "<h1>".$this->siteName."</h1>";
}
}
This allows for a horizontal spreading of your source code.
Your code does work with a few tweaks. You need to concatenate your string's h1 tags with your variable, quotes can't be around the entire thing.
Other than that, just create an instance of the class and trying it out.
class One {
protected $siteName;
function __construct() {
$this->siteName = 'Example';
}
public function build() {
$two = new Two();
return $two->build();
}
}
class Two extends One {
public function build() {
return "<h1>".$this->siteName."</h1>";
}
}
$taco = new Two();
echo $taco->build();
This will return <h1>Example</h1>
I have the following:
config.php
class myObject {
public $_access_token;
public function __construct() {
$this->_access_token = '';
}
}
I need to pull the _access_token. Is this the best way to do it?
index.php:
require("config.php");
class gotime
{
public function getAccessToken(){
$obj = new myObject();
return $obj->_access_token;
}
Can I create the class outside the public function? It seems so inefficient to be putting a class creation in every function. I have another 12 variables I need to pull across files and I would like to set them in one place.
You can also use them like this.
class myObject
{
public static $_access_token;
public function __construct()
{
self::$_access_token = 'some value';//set the value you want
}
}
echo myObject::$_access_token;
I'm new to object oriented php. And if there are no functions in the method testing() in the HumanClass, should i declare them as abstract?
<?php
class HumanClass
{
private $legs;
private $hands;
public function __construct($legs, $hands)
{
$this->legs = $legs;
$this->hands = $hands;
}
public function testing()
{
}
}
class StudentClass extends HumanClass
{
private $books;
public function __construct($legs, $hands, $books)
{
parent::__construct($legs, $hands);
$this->books = $books;
}
public function testing()
{
echo "StudentClass called.";
}
}
function callClass(HumanClass $c)
{
$c->testing();
}
$example = new StudentClass(4, 2, 1);
callClass($a);
?>
Is it possible to have something like this?
echo $a->testing();
instead of having another method to call testing().
Given the code that you give, it's far from clear what the testing() function is supposed to do other than just exist for you to try things. The answer to that will also determine whether the versions in the baseclass should remain there as empty function.
There are other options, too, e.g. that the derived class first invokes the baseclass (extending), or that the baseclass doesn't contain an abstract or concrete such function but only the derived one does. Which to choose is up to the informed programmer to decide.
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.
Is there a way to do something like this:
class Test {
if(!empty($somevariable)) {
public function somefunction() {
}
}
}
I know this might not be best practice, but I need to do this for a very specific problem I have, so is there anyway to do it?
I just want that function to be included in the class if that variable (which is tied to a URL param) is not empty. As it is written now, I get Error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
Thanks!
It depends on the your specific use case, and I don't have enough info to give a specific answer, but I can think of one possible fix.
Extend the class, using an if statement. Put everything except the one function in AbstractTest.
<?php
abstract class AbstractTest
{
// Rest of your code in here
}
if (!empty($somevariable)) {
class Test extends AbstractTest {
public function somefunction() {
}
}
} else {
class Test extends AbstractTest { }
}
Now, the class Test only has the method somefunction if $somevariable isn't empty. Otherwise it directly extends AbstractTest and doesn't add the new method.
Call the required function if the variable is not empty.
<?php
class Test {
public function myFunct() {
//Function description
}
}
$oTest = new Test();
if(!empty($_GET['urlParam'])) {
oTest->myFunc();
}
?>
class Test {
public function somefunction() {
}
}
is all you need actually.
Please note that a function inside a class is called 'method'.
AFAIK you cannot have a condition out of the method in class scope (if that flows)
Class Test {
if (empty($Var)){
public function Test_Method (){
}
}
}
Will not work. Why not have it constantly exisisting but only call the method when it's needed?
Example:
Class Test {
public function Some_Method(){
return 23094; // Return something for example purpose
}
}
Then from your PHP:
$Var = ""; // set an empty string
$Class = new Test();
if (empty($Var)){
echo $Class->Some_Method(); // Will output if $Var is empty
}
Perhaps you trying to validate a string within OOP scope, then take this example:
Class New_Test {
public $Variable; // Set a public variable
public function Set(){
$This->Variable = "This is not empty"; // When calling, $this->variable will not be empty
}
public function Fail_Safe(){
return "something"; // return a string
}
}
Then out of Scope:
$Class = new New_Test();
if (empty($Class->Variable)){
$Class->Fail_Safe();
} // Call failsafe if the variable in OOP scope is empty