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.
Related
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 two php classes in two separate files:
File Name : ld.php
<?php
class LandDetail_Model{
public function __construct() {}
private $id;
public $pId;
private $bigha;
private $katha;
function setId($id) { $this->id = $id; }
function getId() { return $this->id; }
function setPId($pId) { $this->pId = $pId; }
function getPId() { return $this->pId; }
function setBigha($bigha) { $this->bigha = $bigha; }
function getBigha() { return $this->bigha; }
function setKatha($katha) { $this->katha = $katha; }
?>
File Name :Land_Detail.php
<?php
require_once '../models/ld.php';
class LandDetail extends LandDetail_Model{
public function __construct() {
parent::__construct();
}
public function DoSomething(){
echo "This is a value from Parent".$this->getPId();
}
}
?>
Now in SomeFile.php I am doing something like this.
<?php
include 'Land_Detail.php';
$ld = new LandDetail();
$ld->setPId(10001);
$ld->DoSomething();
?>
Why $this->getPId() is always returning null value? What is wrong with my code here? What is the correct way to extend a class in php from different file?
Why you write:
$ld->setPId = 10001;
Instead of
$ld->setPId(10001);
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 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
Can anyone explain me why this code does not work (the content $this->_string) is empty?
<?php
class WordProcessor
{
public $_string = '';
public function __constructor($text)
{
$this->_string = $text;
}
public function toLowerCase()
{
$this->_string = strtolower($this->_string);
return $this;
}
public function trimString()
{
echo $this->_string;
$this->_string = trim($this->_string);
return $this;
}
public function capitalizeFirstLetter()
{
$this->_string = trim($this->_string);
return $this;
}
public function printResult()
{
echo $this->_string;
}
}
$data = new WordProcessor("here Are some words! ");
$data->trimString()->toLowerCase()->capitalizeFirstLetter()->printResult();
Use construct instead of constructor?
Its
public function __construct($text)
not __constructor(..)