I am trying to write a small engine in PHP to handle data but i keep on getting this error:
Fatal error: Class 'Factory\dataHandler' not found in /Applications/MAMP/htdocs/Imperial/lp/php/dataPhraserController.php on line 12
Been now trying to find a way of solving the problem but not luck.....
My code:
DataFactory.php
<?php
namespace Factory;
class dataHandler{
protected $firstName;
protected $lastName;
protected $email;
protected $confirmEmail;
protected $phoneNumber;
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
public function setEmail($email)
{
$this->email = $email;
}
public function setConfirmEmail($confirmEmail)
{
$this->confirmEmail = $confirmEmail;
}
public function setPhoneNumber($phoneNumber)
{
$this->phoneNumber = $phoneNumber;
}
public function getFirstName()
{
var_dump($this->firstName);
}
public function getLastName()
{
return $this->lastName;
}
public function getEmail()
{
return $this->confirmEmail;
}
public function getPhoneNumber()
{
return $this->phoneNumber;
}
}
dataPharserController.php:
<?php
namespace PhraserController;
use Factory\dataHandler;
class DataPhraser
{
private $object;
public function __construct()
{
$this->object = new dataHandler;
}
public function pharseFirstName()
{
if(!isset($_POST['first_name']))
{
$this->object->setFirstName($firstName = null);
var_dump($_POST['first_name']);
}else{
$this->object->setFirstName($firstName = $_POST['first_name']);
}
}
}
$test = new DataPhraser();
$test->pharseFirstName();
The idea is to collect data from a submitted html form, can someone help me.. thx
Place this in one of your config files that are loaded before everything
DEFINE('LIB_DIR', '/path_to_library_dir_of_project/');
function AutoloadDefault($ClassName)
{
$File = LIB_DIR.'/' . $ClassName. '.php';
// echo 'Default:'.$File.'<br/>';
if (file_exists($File))
{
require_once($File);
}
}
spl_autoload_register('AutoloadDefault');
Then just point to right LIB_DIR folder. spl_autoload_register will load the class file
Related
Please check my issue i make a script but $contractempsal variable didn't work. i want employee salary but i got error. please check.
Follow is my code:-
<?php
class baseemp {
protected $firstname;
protected $lastname;
public function getfullname() {
return $this->firstname .''. $this->lastname;
}
public function __construct($first,$last) {
$this->firstname="$first";
$this->lastname="$last";
}
}
class fulltimeemp extends baseemp {
public $monthlysal;
public function monthlysalary() {
$this->monthlysal/12;
}
}
class contractemp extends baseemp {
public $hourlyrate;
public $totalhours;
public function monthlysalaryy() {
$this->hourlyrate * $this->totalhours;
}
public function __construct($hourrate,$totalhoursd){
$this->hourlyrate="$hourrate";
$this->totalhours="$totalhoursd";
}
}
$fulltimeemployee = new fulltimeemp("kannu"," singh");
$contractempsal = new contractemp("400","5");
echo $fulltimeemployee->getfullname();
echo $contractempsal->getfullname();
?>
So, this is my code. please check and tell me where i am wrong Thanks
Please check my issue i make a script but $contractempsal variable didn't work. i want employee salary but i got error. please check.
You need to update __construct function of constractemp class for then call parent::__construct to set first and last name.
<?php
class baseemp {
protected $firstname;
protected $lastname;
public function getfullname() {
return $this->firstname .''. $this->lastname;
}
public function __construct($first,$last) {
$this->firstname="$first";
$this->lastname="$last";
}
}
class fulltimeemp extends baseemp {
public $monthlysal;
public function monthlysalary() {
$this->monthlysal/12;
}
}
class contractemp extends baseemp {
public $hourlyrate;
public $totalhours;
public function monthlysalary() {
return $this->hourlyrate * $this->totalhours;
}
public function __construct($first,$last, $hourrate, $totalhoursd){
parent::__construct($first,$last);
$this->hourlyrate="$hourrate";
$this->totalhours="$totalhoursd";
}
}
$fulltimeemployee = new fulltimeemp("kannu"," singh");
$contractempsal = new contractemp("kannu"," singh", "400","5");
echo $fulltimeemployee->getfullname(); // display kannu singh
echo $contractempsal->getfullname(); // display kannu singh
echo $contractempsal->monthlysalary(); // display 2000
Here is my sample code Class User but not working when I added the static method with the public methods:
<?php
namespace App\Classic;
class User
{
public $username;
public static $upassword;
public $age;
public $message;
public function username($username)
{
$this->username = $username;
echo $this->username."<br>";
return $this;
}
public static function password($upassword)
{
self::$upassword = $upassword;
echo self::$upassword."<br>";
}
public function age($age)
{
$this->age = $age;
echo $this->age."<br>";
return $this;
}
public function message($message)
{
$this->message = $message;
echo $this->message."<br>";
return $this;
}
}
and this is the side effect of chaining method:
$user = new User();
$user::password('secret')
->username('admin')
->age(40)
->message('lorem ipsum');
I dont know what is the logic behind doing this, but still this solution will be helpful.
Try this code snippet here
<?php
namespace App\Classic;
ini_set('display_errors', 1);
class User
{
public $username;
public static $upassword;
public static $currentObject=null;//added this variable which hold current class object
public $age;
public $message;
public function __construct()//added a constructor which set's current class object in a static variable
{
self::$currentObject= $this;
}
public function username($username)
{
$this->username = $username;
echo $this->username . "<br>";
return $this;//added this statment which will return current class object
}
public static function password($upassword)
{
self::$upassword = $upassword;
echo self::$upassword . "<br>";
return self::$currentObject;
}
public function age($age)
{
$this->age = $age;
echo $this->age . "<br>";
return $this;
}
public function message($message)
{
$this->message = $message;
echo $this->message . "<br>";
return $this;
}
}
$user = new User();
$user::password('secret')
->username('admin')
->age(40)
->message('lorem ipsum');
I have been trying to retrieve data from backendless database using PHP but i get this error.
I followed the documentation but there is no much details written.
I have been trying to retrieve data from backendless database using PHP but i get this error.
I followed the documentation but there is no much details written.
Below is my code
<?php
require_once('Plant.php');
use backendless\Backendless;
include "vendor/backendless/autoload.php";
Backendless::initApp('API-KEY', 'API-KEY', 'v1');
$first_contact = Backendless::$Persistence->of('Plant')->findFirst();
$first_contact->getName();
<?php
class Plant {
private $userId;
private $tempLimit;
private $name;
private $lightLimit;
private $lastWatered;
private $humidityLimit;
private $currentTemp;
private $currentHumidity;
private $currentLight;
public function __construct() {
}
public function getName() {
return $thsi->name;
}
public function setName( $name ) {
$this->name = $name;
}
public function getUserId() {
return $thsi->userId;
}
public function setUserId( $userId ) {
$this->userId = $userId;
}
public function gettempLimit() {
return $thsi->tempLimit;
}
public function setTempLimit( $tempLimit ) {
$this->tempLimit = $tempLimit;
}
public function getLightLimit() {
return $thsi->lightLimit;
}
public function setLightLimit( $lightLimit ) {
$this->lightLimit = $lightLimit;
}
public function getLastWatered() {
return $thsi->lastWatered;
}
public function setLastWatered( $lastWatered ) {
$this->lastWatered = $lastWatered;
}
public function getHumidityLimit() {
return $thsi->humidityLimit;
}
public function setHumidityLimit( $humidityLimit ) {
$this->humidityLimit = $humidityLimit;
}
public function getCurrentTemp() {
return $thsi->currentTemp;
}
public function setCurrentTemp( $currentTemp ) {
$this->currentTemp = $currentTemp;
}
}
I am not getting an output for the following piece of code. I cant seem to find the reason for this error.
Heres the code for the config.php
<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS','somepwd');
define('DB_NAME','prac');
class Play
{
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
private $connection;
function __construct()
{
$this->open_connection();
}
public function full_name() {
if(isset($this->first_name) && isset($this->last_name)) {
return $this->first_name . " " . $this->last_name;
} else {
return "";
}
}
public function open_connection()
{
$this->connection=mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
}
public static function find_all() {
return self::find_by_sql("SELECT * FROM users");
}
public static function find_by_sql($sql)
{
$result_set=mysql_query($sql,$this->connection);
$object_array=array();
while($row=mysql_fetch_array($result_set))
{
$object_array[]=self::instantiate($row);
}
return $object_array;
}
private static function instantiate($record)
{
$object=new self;
foreach ($record as $k => $value) {
if($object->has_attribte($k))
{
$object->$k=$value;
}
}
return object;
}
private function has_attribte($att)
{
$some=get_object_vars($this);
return array_key_exists($att, $some);
}
}
$play=new Play();
?>
Heres the code for the index.php
<?php
include('config.php');
$user=Play::find_all();
echo $user->username;
?>
So i am basically trying to instantiate an object to a query result but it is not working. I get no output. Please help.
This is kind of embarrassing but I having problems calling a method from a class in PHP, this is how it goes: I have created a class persona with its respectives properties, the code is
<?php
class persona {
private $id;
private $nombre; //varchar(50)
private $correo; //varchar(50)
private $especialidad; //varchar(50)
private $nacionalidad; //varchar(50)
private $sueldo; //float
private $isss; //float
private $afp; //float
//getter
public function getId() {
return $this->id;
}
public function getNombre() {
return $this->nombre;
}
public function getCorreo() {
return $this->descripcion;
}
public function getEspecialidad(){
return $this->especialidad;
}
public function getNacionalidad(){
return $this->nacionalidad;
}
public function getSueldo(){
return $this->sueldo;
}
public function getIsss(){
return $this->isss;
}
public function getAfp(){
return $this->afp;
}
//setter
public function setNombre($nombre) {
$this->nombre = $nombre;
}
public function setCorreo($correo) {
$this->correo = $correo;
}
public function setEspecialidad($especialidad){
$this->especialidad=$especialidad;
}
public function setNacionalidad($nacionalidad){
$this->nacionalidad=$nacionalidad;
}
public function setSueldo($sueldo){
$this->sueldo=$sueldo;
}
public function setIsss($isss){
$this->isss=$isss;
}
public function setAfp($afp){
$this->afp=$afp;
}
public function __construct($nombre, $correo, $especialidad, $nacionalidad, $sueldo, $isss, $afp, $id=null) {
$this->nombre = $nombre;
$this->correo = $descipcion;
$this->especialidad = $especialidad;
$this->nacionalidad = $nacionalidad;
$this->sueldo = $sueldo;
$this ->isss = $isss;
$this->afp = $afp;
$this->id = $id;
}
public function mostrar(){
$mensaje="hola";
return $mensaje;
}
}
?>
as you can see at the end of the code I have created a function called mostrar, the only purpose of this is to show a message, now I want to call this method from a diferent class, my code is
<?php
require_once('persona.php');
class prueba{
private $person;
public function __construct(){
$person= new persona("el nombre","el correo", "la especialidad", "la nacionalidad", "el sueldo", "el isss", "el afp");
mostrando();
}
public function mostrando(){
$person->mostrar();
}
}
?>
but when I debug it doesn' show anything in the browser, I want to display the message from the class person.php, could you please tell me what is the problem in my code?
Variable scope:
$person !== $this->person
class prueba{
private $person;
public function __construct(){
$this->person= new persona("el nombre","el correo", "la especialidad", "la nacionalidad", "el sueldo", "el isss", "el afp");
$this->mostrando();
}
public function mostrando(){
echo $this->person->mostrar();
}
}
$this->person is an object property, accessible from all methods in the class.
$person is a local variable, accessible only from the method/function in which it is defined (unless passed as an argument to other methods/functions)