{"id":18278723,"url":"https://github.com/marcos4503/backend-response-builder","last_synced_at":"2025-04-09T04:41:07.909Z","repository":{"id":165741457,"uuid":"636909952","full_name":"marcos4503/backend-response-builder","owner":"marcos4503","description":"This is a PHP library that provides an alternative response system to REST or SOAP standards so that Backends written in PHP can return JSON encapsulated responses to Clients. The core idea of this library is to provide a highly standardized, well-structured and typed way to do this, so the Clients can always trust the Backend responses.","archived":false,"fork":false,"pushed_at":"2023-05-25T19:50:18.000Z","size":67,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T22:53:00.035Z","etag":null,"topics":["alternative","api","backend","builder","framework","json","php","response","typed"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcos4503.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-06T00:31:13.000Z","updated_at":"2024-06-28T11:13:38.000Z","dependencies_parsed_at":"2023-06-03T00:45:44.792Z","dependency_job_id":null,"html_url":"https://github.com/marcos4503/backend-response-builder","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcos4503%2Fbackend-response-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcos4503%2Fbackend-response-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcos4503%2Fbackend-response-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcos4503%2Fbackend-response-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcos4503","download_url":"https://codeload.github.com/marcos4503/backend-response-builder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247980825,"owners_count":21027803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alternative","api","backend","builder","framework","json","php","response","typed"],"created_at":"2024-11-05T12:25:49.169Z","updated_at":"2025-04-09T04:41:07.886Z","avatar_url":"https://github.com/marcos4503.png","language":"PHP","readme":"# Backend Response Builder\n\nBackend Response Builder is a library written for PHP that has the task of being an alternative for you who don't want (or can't) use systems like SOAP or REST, and just want to use HTTPS + PHP + GET/POST returning JSON to build your Backend APIs.\n\nThis library provides a standardized, reliable, and typed way to construct your JSON responses that will be returned to Clients that consume your PHP APIs. Backend Response Builder has a set of rules to standardize the way in which JSON responses will be sent by your PHP APIs. With this library you must declare each variable that will be in the JSON response, as well as the type of each variable. That way, no matter what happens in your script, the same variables will be returned in any type of response. Furthermore, thanks to typing, this library will guarantee that each variable returned in your script's response JSON will always be of the same type, that is, the Client will always be able to trust that each variable present in the response JSON will ALWAYS be of the same type, which eliminates the need for additional code to check the type of each variable present in the response JSON.\n\nSo far, this library is only able to build responses in JSON. XML is not supported.\n\n# How it works?\n\nNow, let's see how to build the JSON responses that will be returned to Clients that consume your PHP APIs that use this library. First, you should start by including this library to your script that you want to use it.\n\nFirst you need to clone this repository. Open the downloaded file and go to the \"Backend-Response-Builder-Source\" folder then copy the \"backend-response-builder.php\" file and place it somewhere on your website. The next step is to reference the library in your PHP script so that you can use the library's code within your PHP code. To do this, place the code below at the beginning of your PHP scripts where you plan to use this library. But remember to change the path to correctly reference the PHP library file!\n\n```php\n\u003c?php\n\ninclude_once(\"../../backend-response-builder.php\");\n\n?\u003e\n```\n\nNow, to start with, right after you include the PHP library file, at the BEGINNING of your PHP script, you must instantiate an object of type `ResponseBuilder` and then start declaring the variables you intend to use in your JSON response. Declaring variables can be done using the `DeclareVariablePrimitive()` method of the object `ResponseBuilder`. See example code below...\n\n```php\n\u003c?php \n\n//Prepare the response\n$response = new ResponseBuilder();\n\n//Declare the variables\n$response-\u003eDeclareVariablePrimitive(\"brand\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"model\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"year\", \"INT\");\n\n?\u003e\n```\n\nBy declaring a variable you are informing the Backend Response Builder that you plan to use that variable in your response. You can only edit the value of variables that have been declared. After declaring all the variables you plan to use in your JSON response, you can start your PHP script logic as you normally would.\n\nDuring the logic of your PHP script, you can define values for each variable declared for the Backend Response Builder! The values you set for each variable will be displayed in the JSON response the library creates. To define new values for the variables, you can use the `SetVariablePrimitiveValue()` method. See the example below...\n\n```php\n\u003c?php\n\n//Prepare the response\n$response = new ResponseBuilder();\n\n//Declare the variables\n$response-\u003eDeclareVariablePrimitive(\"brand\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"model\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"year\", \"INT\");\n\n\n/*\n *\n * RUN THE LOGIC OF THE PHP SCRIPT...\n * \n*/\n\n//Set the values for variables\n$response-\u003eSetVariablePrimitiveValue(\"brand\", \"honda\");\n$response-\u003eSetVariablePrimitiveValue(\"model\", \"civic\");\n$response-\u003eSetVariablePrimitiveValue(\"year\", 2017);\n\n?\u003e\n```\n\n\u003cb\u003eRemember:\u003c/b\u003e Variable declarations should ALWAYS be done at the BEGINNING of your PHP script. After editing the value of any variable it is no longer possible to declare anything else. This is done on purpose and is a design rule of this library. This is done so that any variables that will be used in the response are declared at the beginning of the script and avoid messes, such as new variables being created in the middle of the script, which will cause variables that may not always appear in JSON responses.\n\nAfter declaring all the variables that you will need to be returned in the JSON response, executing your script's PHP logic and supplying the values for those variables, you must call the `BuildAndPrintTheResponseToClient()` method. This method will take everything and build a JSON response and print it as a result so that the Client consuming your PHP API can read that response and process it. See sample code below...\n\n```php\n\u003c?php\n\ninclude_once(\"backend-response-builder.php\");\n\n//Prepare the response\n$response = new ResponseBuilder();\n\n//Declare the variables\n$response-\u003eDeclareVariablePrimitive(\"brand\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"model\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"year\", \"INT\");\n\n\n//Set the values for variables\n$response-\u003eSetVariablePrimitiveValue(\"brand\", \"honda\");\n$response-\u003eSetVariablePrimitiveValue(\"model\", \"civic\");\n$response-\u003eSetVariablePrimitiveValue(\"year\", 2017);\n\n\n//Print the response to Client\n$response-\u003eBuildAndPrintTheResponseToClient();\n\n?\u003e\n```\n\nThis PHP code will produce the following response (in text plain) when accessed by some Client...\n\n```md\nnoResponseHeaderDefined\n\u003cbr/\u003e\n{\n    \"brand\": \"honda\",\n    \"model\": \"civic\",\n    \"year\": 2017,\n    \"processingTime\": \"0.00003194808960\"\n}\n```\n\nThis text response will be obtained by any Client that consumes this PHP script. Just remember not to produce text in your PHP script using methods like `echo()` and `print`, as any additional text generated by your PHP script may conflict with this text, which will hinder reading by Clients, causing problems such as errors or bugs.\n\n\u003cb\u003eBut this is not all!\u003c/b\u003e With this library, you will also be able to include Arrays, Objects and other things in your JSON responses! Keep reading to see it all!\n\n# Header and Body\n\nFirst, before we start this topic. Keep in mind that the Header and Body we are going to talk about has **NOTHING** relationed with the Head and Body of your HTML pages! The Header and Body we are going to talk about here is only linked to the JSON responses produced by this library! :)\n\nYou may have noticed that there is a line of `noResponseHeaderDefined` and `\u003cbr/\u003e` text before you actually see the JSON. The line containing the `noResponseHeaderDefined` is called the **Response Header**! The line containing the `\u003cbr/\u003e` is called the **Response Separator**. The JSON is called the **Response Body**! Now, let's understand each of these parts that make up the responses produced by this library!\n\n\u003ch3\u003eResponse Header\u003c/h3\u003e\n\nThe Header is always the first line of responses produced by this library. The `noResponseHeaderDefined` header is displayed when you have not defined any headers for the response.\n\nYou can define a header using the `SetSuccessHeader()` method, as in the example below...\n\n```php\n$response-\u003eSetSuccessHeader(true);\n```\n\nIf there was any problem like invalid input, or some problem while runing your PHP script you should pass `FALSE` to this method, and the `error` header will be set. If everything went as expected, then you should pass `TRUE` to this method and the `success` header will be set!\n\nThe header is the first thing the Client should read when receiving a response created by this library. This way, just by reading the header, the Client will know if everything went well, or if there was any problem in the execution of the API (such as invalid input, offline database, execution error, etc). That way, if the header reports that there was an error on running the API, the Client doesn't even need to read the JSON to find out about the problem.\n\nUsing the `SetCustomHeader()` method, you can define a COMPLETELY custom header however you like! You can use this method to set things other than `error` or `success` in your responses! See the code below for example...\n\n```php\n$response-\u003eSetCustomHeader(\"offline-database\");\n```\n\n\u003ch3\u003eResponse Body\u003c/h3\u003e\n\nThe Body of responses built by this library is the JSON that contains all the variables you declared, and their respective values! Nothing more than that! It's just a standard JSON that includes all the variables you declare, and their values that you set when running your script. The Client can read JSON from responses built by this library just like it would read any other JSON code!\n\n\u003ch3\u003eResponse Separator\u003c/h3\u003e\n\nThe Separator is always the second line of responses created by this library. The separator is ALWAYS `\u003cbr/\u003e` and this serves only as a delimiting zone so that the Client always knows where the response header ends and where the response body begins. That way, the Client can easily separate the Header from the JSON when reading responses created by this library.\n\n\u003ch3\u003eIn Summary...\u003c/h3\u003e\n\n```md\nnoResponseHeaderDefined                          \u003c- Response Header\n\u003cbr/\u003e                                            \u003c- Response Separator\n{                                                '''|\n    \"brand\": \"honda\",                               |\n    \"model\": \"civic\",                               |\n    \"year\": 2017,                                   | \u003c- Response Body (JSON)\n    \"processingTime\": \"0.00003194808960\"            |\n}                                                ...|\n```\n\n# How the Client should read the responses\n\nAs previously mentioned, when calling the `BuildAndPrintTheResponseToClient()` method, the library will process all the declared variables and their respective values and then generate the response with the Header and Body structure that you saw above.\n\nFor a Client to read the response obtained from this library, it must first access your PHP API, sending the POST/GET data (if necessary) and as it would normally access any PHP page. When accessing, it then must obtain the text resulting from the request to its PHP API. The text resulting from the request is exactly the response produced by your PHP API, that is, the text generated when calling the `BuildAndPrintTheResponseToClient()` method of this library!\n\nNow, let's see step by step what the Client must do to read the response provided by this library correctly!\n\n\u003cb\u003eFirst Step:\u003c/b\u003e With the text resulting from the request ready, the first thing the Client must do is SPLIT this text using the `\u003cbr/\u003e` separator. But this separation should only separate the FIRST occurrence of the `\u003cbr/\u003e` separator. This way, the Client will obtain an array with `2` elements, where element `0` is the Header of the response, and element `1` is the Body (JSON) from the response. For example...\n\nThis can be done with Javascript as follows...\n\n```javascript\n//Split the response to get the Header and Body (JSON)\nvar splitedResponse = requestText.split(/\u003cbr\\/\u003e(.*)/).filter(part =\u003e part);\nvar header = splitedResponse[0];\nvar body = splitedResponse[1];\n```\n\nAnd it can be done with C# as follows...\n\n```c#\n//Split the response to get the Header and Body (JSON)\nstring splitedResponse = requestText.Split(new string[] { \"\u003cbr/\u003e\" }, 2, StringSplitOptions.None);\nstring header = splitedResponse[0];\nstring body = splitedResponse[1];\n```\n\n\u003cb\u003eSecond Step:\u003c/b\u003e Now the Client must read the Header to get a quick summary of what the response it gets from the PHP API is all about! So, if the Header CONTAINS what the Client expects to be a success message, then ONLY after that it should start reading the Body (JSON) of the response. It can do this by deserializing the JSON and converting it into an object to read each variable present for example.\n\nThis can be done with Javascript as follows...\n\n```javascript\n//Split the response to get the Header and Body (JSON)\nvar splitedResponse = requestText.split(/\u003cbr\\/\u003e(.*)/).filter(part =\u003e part);\nvar header = splitedResponse[0];\nvar body = splitedResponse[1];\n\n//If the header contains the \"success\" message, read the JSON\nif (header.includes(\"success\") == true){\n\n    //read the JSON...\n    var jsonResponse = JSON.parse(body);\n\n}\n```\n\nAnd it can be done with C# as follows...\n\n```c#\n//Split the response to get the Header and Body (JSON)\nstring splitedResponse = requestText.Split(new string[] { \"\u003cbr/\u003e\" }, 2, StringSplitOptions.None);\nstring header = splitedResponse[0];\nstring body = splitedResponse[1];\n\n//If the header contains the \"success\" message, read the JSON\nif (header.Contains(\"success\") == true){\n\n    //read the JSON...\n\n}\n```\n\n\u003cb\u003eIMPORTANT!\u003c/b\u003e It is important that the Header comparison is done by checking whether the Header string **CONTAINS** the expected success message, and **never** checking whether the Header string is **EQUAL** to an expected message. This is because, as the Header string is the first line, it may contain unexpected characters like BOM characters, \"\\n\" etc. Therefore, when reading and comparing the Header string, never compare using **EQUALITY**, but only check that the Header string **CONTAINS** the expected success message!\n\n\u003cb\u003eLast Step:\u003c/b\u003e If the Header string contains the expected success message, then the Client is ready to read the JSON (Body) string of the response! After deserializing the JSON, the Client can start reading the variables obtained from your PHP API response! That's all you need to know to program your Clients and Frontends to read the responses produced by this library!\n\n# Adding Objects (Classes) to response\n\nBefore we move on to the more advanced part of this library, you need to learn about how to add Objects or Classes to the response JSON!\n\nAn Object, in JSON is like another JSON code inside a variable. This JSON code that we call an Object can contain more variables within it. It's a great way to represent multiple instances of the same Class, for example, so that the Client can read the information in a more organized and structured way!\n\nFor example, let's say we want to return in our API, the name \"Parzival James\" and his 2 cars that he owns, but each car must be represented by an object. We can do this with this library as follows...\n\nFirst, we declare the variable names and types. Next we need to use method `DeclareStructuredClass()` to declare a Class in which we will create two instances to represent Parzival two cars. Then we use method `DeclareVariablePrimitiveInClass()` to add \"properties\" to this Class, which we will use to inform the brand and model of each car. We can do this in the following way...\n\n```php\n\u003c?php\n\n$response-\u003eDeclareVariablePrimitive(\"name\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"car1\", \"OBJECT\");\n$response-\u003eDeclareVariablePrimitive(\"car2\", \"OBJECT\");\n\n$response-\u003eDeclareStructuredClass(\"Car\");\n$response-\u003eDeclareVariablePrimitiveInClass(\"Car\", \"brand\", \"STRING\");\n$response-\u003eDeclareVariablePrimitiveInClass(\"Car\", \"model\", \"STRING\");\n\n?\u003e\n```\n\nNow, we need to create two instances of the `Car` class we created, for this, use the `CreateNewObjectInstanceOfClass()` method to create the instances of the class then use method `SetVariablePrimitiveValueInInstancedObject()` to inform the brand and model of the cars! This can be done as follows...\n\n```php\n\u003c?php\n\n$response-\u003eSetVariablePrimitiveValue(\"name\", \"Parzival James\");\n\n$car1Reference = $response-\u003eCreateNewObjectInstanceOfClass(\"Car\");\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($car1Reference, \"brand\", \"Honda\");\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($car1Reference, \"model\", \"Civic\");\n\n$car2Reference = $response-\u003eCreateNewObjectInstanceOfClass(\"Car\");\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($car2Reference, \"brand\", \"Toyota\");\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($car2Reference, \"model\", \"Corolla\");\n\n?\u003e\n```\n\nNow, we already have two instances of the `Car` class with data for each car, but these instances won't appear in the response unless we link these instantiated Objects to some variable that appears in the response! In this case, we have the `car1` and `car2` variables, so we only need to link the two instantiated Objects to these variables and we will have both Objects appearing in our API response!\n\n```php\n\u003c?php\n\n$response-\u003eLinkObjectToVariablePrimitive(\"car1\", $car1Reference);\n$response-\u003eLinkObjectToVariablePrimitive(\"car2\", $car2Reference);\n\n?\u003e\n```\n\nThis is the final code!\n\n```php\n\u003c?php\n\n//Prepare the response\n$response = new ResponseBuilder();\n$response-\u003eSetSuccessHeader(true);\n\n$response-\u003eDeclareVariablePrimitive(\"name\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"car1\", \"OBJECT\");\n$response-\u003eDeclareVariablePrimitive(\"car2\", \"OBJECT\");\n\n$response-\u003eDeclareStructuredClass(\"Car\");\n$response-\u003eDeclareVariablePrimitiveInClass(\"Car\", \"brand\", \"STRING\");\n$response-\u003eDeclareVariablePrimitiveInClass(\"Car\", \"model\", \"STRING\");\n\n$response-\u003eSetVariablePrimitiveValue(\"name\", \"Parzival James\");\n\n$car1Reference = $response-\u003eCreateNewObjectInstanceOfClass(\"Car\");\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($car1Reference, \"brand\", \"Honda\");\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($car1Reference, \"model\", \"Civic\");\n\n$car2Reference = $response-\u003eCreateNewObjectInstanceOfClass(\"Car\");\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($car2Reference, \"brand\", \"Toyota\");\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($car2Reference, \"model\", \"Corolla\");\n\n$response-\u003eLinkObjectToVariablePrimitive(\"car1\", $car1Reference);\n$response-\u003eLinkObjectToVariablePrimitive(\"car2\", $car2Reference);\n\n//Print the response to Client\n$response-\u003eBuildAndPrintTheResponseToClient();\n\n?\u003e\n```\n\nAnd this is the response produced by this code!\n\n```md\nsuccess\n\u003cbr/\u003e\n{\n    \"name\": \"Parzival James\",\n    \"car1\": {\n        \"brand\": \"Honda\",\n        \"model\": \"Civic\"\n    },\n    \"car2\": {\n        \"brand\": \"Toyota\",\n        \"model\": \"Corolla\"\n    },\n    \"processingTime\": \"0.00004386901855\"\n}\n```\n\nAnd that's how you add Objects to your responses, using this library! So, just to resume it all up, you need to declare a Class and add variables to this Class. The Class will serve as a template for the Objects, so you need to create an Object instance of this Class, and save the reference to this instance. With the reference, you can edit the variables of the instantiated Object and then link this Object to a Variable or Array of type OBJECT so that the instantiated Object appears in the generated response!\n\nNow, let's understand every detail of this library with regard to declaring and editing variables, which will be present in the responses generated by this library!\n\n# Declaring variables to response and editing their values\n\nIf you've read everything up to here, you've already understood the basics of using this library, however, in this topic you'll understand the details about declaring variables and editing their values!\n\nAs you know, first, for a variable to appear in the response generated by this library, you need to declare it. You will only be able to add variables and edit their values to your API response if you declare them. And it is recommended that you declare all the variables that you intend to use in your response, right at the beginning of your PHP script.\n\n\u003ch3\u003eDeclaring Primitive Variables\u003c/h3\u003e\n\nPrimitive variables are basic variables and can be of type `STRING`, `INT`, `FLOAT`, `BOOL` and `OBJECT`. When declaring a variable, you need to inform the type of value that will be stored in that variable. A variable can only hold values of the type it was created to store. If you declare a variable named \"year\" and type \"INT\", it can only store integer numbers. If you try to store values of a different type than the declared variable, you will see error messages and the value will not be stored in it.\n\n- \u003cb\u003eSTRING:\u003c/b\u003e Variables of this type can only store text.\n- \u003cb\u003eINT:\u003c/b\u003e Variables of this type can store only integers (INT or LONG).\n- \u003cb\u003eFLOAT:\u003c/b\u003e Variables of this type can store integers or decimals (INT, LONG, FLOAT or DOUBLE).\n- \u003cb\u003eBOOL:\u003c/b\u003e Variables of this type can only hold boolean values (TRUE or FALSE).\n- \u003cb\u003eOBJECT:\u003c/b\u003e Variables of this type can only hold Objects instantiated using the `CreateNewObjectInstanceOfClass()` method as you learned above..\n\nTo declare a variable Primitive that will appear in the response, use the `DeclareVariablePrimitive()` method, as in the example below. You must inform the name of the variable and the type of value that will be stored.\n\n```php\n\u003c?php\n\n$response-\u003eDeclareVariablePrimitive(\"name\", \"STRING\");\n$response-\u003eDeclareVariablePrimitive(\"year\", \"INT\");\n\n?\u003e\n```\n\n\u003ch3\u003eDeclaring Array Variables\u003c/h3\u003e\n\nArray variables follow the same concept as Primitive variables, the difference is that an Array variable stores several values of a given type. An Array variable can be of type `STRING`, `INT`, `FLOAT`, `BOOL` and `OBJECT`. Like Primitive variables, an Array variable can only hold values of the type it was declared to store. For example, if you declare an Array variable named \"participants\" and type \"STRING\", you can only store values of type text. If you try to enter values of another type, you will see error messages and the values will not be saved.\n\nTo declare a variable Array that will appear in the response, use the `DeclareVariableArray()` method, as in the example below. You must inform the name of the variable and the type of value that will be stored.\n\n```php\n\u003c?php\n\n$response-\u003eDeclareVariableArray(\"brands\", \"STRING\");\n$response-\u003eDeclareVariableArray(\"owners\", \"OBJECT\");\n\n?\u003e\n```\n\n\u003ch3\u003eDeclaring Classes\u003c/h3\u003e\n\nClasses act as a template to easily create Objects using this library. You saw this in the previous topic. To declare a class you must simply call method `DeclareStructuredClass()`, informing the name of the class.\n\n```php\n\u003c?php\n\n$response-\u003eDeclareStructuredClass(\"Car\");\n\n?\u003e\n```\n\nTo add a variable Primitive to this Class, use the `DeclareVariablePrimitiveInClass()` method. It works exactly like the `DeclareVariablePrimitive()` method, however, it only adds variables to Classes.\n\n```php\n\u003c?php\n\n$response-\u003eDeclareVariablePrimitiveInClass(\"Car\", \"model\", \"STRING\");\n\n?\u003e\n```\n\nTo add a variable Array to this Class, use the `DeclareVariableArrayInClass()` method. It works exactly like the `DeclareVariableArray()` method, however, it only adds variables to Classes.\n\n```php\n\u003c?php\n\n$response-\u003eDeclareVariableArrayInClass(\"Car\", \"years\", \"INT\");\n\n?\u003e\n```\n\nIt's worth remembering that declared Classes will NEVER appear in responses generated by this library! You are also not able to edit variables that are inside a Class. As said before, a Class only works as a Template to create objects! You've already seen in the previous topic, how to create Objects from Classes, but below you'll see more details about how to instantiate Objects from Classes, and then how to edit the variables of these instantiated Objects!\n\n\u003ch3\u003eChanging value of Primitive Variables\u003c/h3\u003e\n\nTo change the value of a Primitive Variable you must use method `SetVariablePrimitiveValue()`. In the method you must inform the name of the variable and the new value you want to define. Using this method you can define values of type `STRING`, `INT`, `FLOAT` or `BOOL`. But you can only assign values that are of the same type that was declared for the variable!\n\n```php\n\u003c?php\n\n$response-\u003eSetVariablePrimitiveValue(\"name\", \"Parzival James\");\n\n?\u003e\n```\n\n\u003ch3\u003eAdding values to an Array Variable\u003c/h3\u003e\n\nTo add values to an Array, you can use the `AddItemToVariableArray()` method. When using this method, you must inform the name of the Array variable and the new value to be added. Using this method you can add values of type `STRING`, `INT`, `FLOAT` or `BOOL`, however, you can only add values that are of the same type as the one declared for the Array variable!\n\n```php\n\u003c?php\n\n$response-\u003eAddItemToVariableArray(\"brands\", \"Mazda\");\n\n?\u003e\n```\n\n\u003cb\u003eNOTE:\u003c/b\u003e You are only able to add values to Arrays from this library. It is not possible to remove values or set new values at a given index, so plan your PHP logic accordingly!\n\n\u003ch3\u003eInstantiating Objects from Classes\u003c/h3\u003e\n\nAs you read in the previous topic, it is possible to instantiate Objects from declared Classes. These Objects can have their variables edited and then it is possible to include these Objects instantiated in your PHP API response!\n\nTo instantiate an Object from a Class, use the `CreateNewObjectInstanceOfClass()` method. You must inform the name of a Class that you have already declared before. When instantiating this Object, you will be creating a copy of this Class, this copy will become an Object. The `CreateNewObjectInstanceOfClass()` method will return you a reference to this instantiated Object. Save this reference as you will need it if you want to edit the Object's variables, or link the instantiated Object to a variable, or add the instantiated Object to an Array!\n\n```php\n\u003c?php\n\n$carObjectReference = $response-\u003eCreateNewObjectInstanceOfClass(\"Car\");\n\n?\u003e\n```\n\n\u003ch3\u003eEditing variables that are inside an instanced Object\u003c/h3\u003e\n\nTo edit a Primitive variable that is inside an instantiated Object, you must use the `SetVariablePrimitiveValueInInstancedObject()` method. It works exactly the same way as the `SetVariablePrimitiveValue()` method, however, it only works to edit variables that are inside an instantiated Object. When calling the `SetVariablePrimitiveValueInInstancedObject()` method, you must pass the reference to the Object instantiated, the name of the variable to be edited and the new value you want to define!\n\n```php\n\u003c?php\n\n$response-\u003eSetVariablePrimitiveValueInInstancedObject($carObjectReference, \"brand\", \"Honda\");\n\n?\u003e\n```\n\nTo add a value to an Array that is inside an instanced Object you must use the `AddItemToVariableArrayInInstancedObject` method. It works exactly the same way as the `AddItemToVariableArray()` method, however, it only works to edit variables that are inside an instantiated Object. When calling the `AddItemToVariableArrayInInstancedObject()` method, you must pass the reference to the Object instantiated, the name of the variable to be edited and the new value you want to add!\n\n```php\n\u003c?php\n\n$response-\u003eAddItemToVariableArrayInInstancedObject($carObjectReference, \"years\", 2018);\n\n?\u003e\n```\n\n\u003ch3\u003eLinking an instanced Object to a Primitive variable\u003c/h3\u003e\n\nAs stated earlier, an instanced Object will not appear in your PHP API response until it is linked to a variable.\n\nTo link an instantiated Object to a Primitive variable, it must be of type `OBJECT`. Then, you must use the `LinkObjectToVariablePrimitive()` method. When calling this method you must pass the variable name and the reference to the instantiated object.\n\n```php\n\u003c?php\n\n$response-\u003eLinkObjectToVariablePrimitive(\"car\", $carObjectReference);\n\n?\u003e\n```\n\nIf you want to link the instantiated Object to a variable that is inside ANOTHER instantiated Object, you must use the `LinkObjectToVariablePrimitiveInInstancedObject()` method. It works like the `LinkObjectToVariablePrimitive()` method, however, it will only work for a primitive variable that is inside another instantiated Object. When calling the `LinkObjectToVariablePrimitiveInInstancedObject()` method, you must pass the reference to the Object instantiated where the variable is, the name of the variable and the reference to the Object instantiated you want to link the variable.\n\n```php\n\u003c?php\n\n$response-\u003eLinkObjectToVariablePrimitiveInInstancedObject($anotherObject, \"car\", $carObjectReference);\n\n?\u003e\n```\n\n\u003ch3\u003eAdding an instantiated Object to an Array\u003c/h3\u003e\n\nTo add an instantiated Object to a Array variable, it must be of type `OBJECT`. Then, you must use the `AddLinkOfObjectToVariableArray()` method. When calling this method you must pass the variable name and the reference to the instantiated object.\n\n```php\n\u003c?php\n\n$response-\u003eAddLinkOfObjectToVariableArray(\"cars\", $carObjectReference);\n\n?\u003e\n```\n\nIf you want to add the instantiated Object to a variable that is inside ANOTHER instantiated Object, you must use the `AddLinkOfObjectToVariableArrayInInstancedObject()` method. It works like the `AddLinkOfObjectToVariableArray()` method, however, it will only work for a Array variable that is inside another instantiated Object. When calling the `AddLinkOfObjectToVariableArrayInInstancedObject()` method, you must pass the reference to the Object instantiated where the variable is, the name of the variable and the reference to the Object instantiated you want to add to array.\n\n```php\n\u003c?php\n\n$response-\u003eAddLinkOfObjectToVariableArrayInInstancedObject($anotherObject, \"cars\", $carObjectReference);\n\n?\u003e\n```\n\n# That is all!\n\nNow you know how to use every aspect of this library! With everything shown here, you can now create JSON responses \u003cb\u003eHOWEVER YOU WANT!\u003c/b\u003e Either with an Array of Objects, with Objects with an Array of Objects, etc. You can create JSON responses however you like with everything you've been taught here, by using the Backend Response Builder! Enjoy the library!\n\n# Support projects like this\n\nIf you liked this Library and found it useful for your projects, please consider making a donation (if possible). This would make it even more possible for me to create and continue to maintain projects like this, but if you cannot make a donation, it is still a pleasure for you to use it! Thanks! 😀\n\n\u003cbr\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://www.paypal.com/donate/?hosted_button_id=MVDJY3AXLL8T2\" target=\"_blank\"\u003e\n        \u003cimg src=\"Backend-Response-Builder-Source/Resources/paypal-donate.png\" alt=\"Donate\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cbr\u003e\n\n\u003cp align=\"center\"\u003e\nCreated with ❤ by Marcos Tomaz\n\u003c/p\u003e","funding_links":["https://www.paypal.com/donate/?hosted_button_id=MVDJY3AXLL8T2"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcos4503%2Fbackend-response-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcos4503%2Fbackend-response-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcos4503%2Fbackend-response-builder/lists"}