can I access private variable in method in same class? - php

I have this controller
class PageController extends Controller
{
private $myid;
public funciton index(){
}
public function viewbyid($id){
$this->myid = $id;
return view('someview');
}
public function getRecord(){
$id = $this->myid;
echo $id; //it would be null here,if I am going to access this method.
return view('anotherview');
}
}

Yes, you can access private varible, in side class any where, Your using OOPS PHP. So may be problem is you might be accessing getRecord() method with diff object.
For Eg:
$obj=new PageController();
$obj->viewbyid("Test");
$obj->getRecord();//Then it will display the result
If your reinitialize an object or creating new object then that object will reallocate memory, so previous saved values will not be present.
For Eg:
$obj=new PageController();
$obj->viewbyid("Test");
$obj=new PageController();//$obj will allocate memory in diff location so your previous values will be initialized to default.
$obj->getRecord();//Then it will display result as null

Related

Cannot access property from another class

Here's the code (didn't include namespaces, routing):
class OneController extends Controller{
public $variable = "whatever";
public function changeVariableAction(){
$this->variable = "whenever";
// any code...
$this->redirectToRoute("class_two_route_name");
}
}
use AppBundle\Controller\OneController;
class Two{
public function otherFunctionAction(){
$reference = new One();
return new Response($reference->variable);
}
}
Why do I see "whatever" instead "whenever"? I know there is no line in the code executing changeVariableAction() but it is being executed when sb enters the route matching this action in class One ???
EDIT:
When I write the scheme outside SF3 I'm OK.
class One{
public $variable = "whatever";
public function changeVariable(){
$this->variable = "whenever";
}
}
class Two{
public function otherFunction(){
$reference = new One();
$reference->changeVariable();
echo $reference->variable;
}
}
$reference2 = new Two();
$reference2->otherFunction();
You are seeing "Whatever" instead of "Whenever" because of this line:
new One();
By calling "new One();" you are creating a new instance of the class "OneController" thus, it will set its default value "whatever" as the function "changeVariableAction" is not being called in your new instance $reference.
After research I can see that in SF (as it is a framework) we don't treat Action functions as typical functions (it's sth about http etc.) so we cannot execute them in another class. What's more, the whole code inside Action function doesn't influence the code outside the Action function. The only way to get new property value is to send them via argument in url (I don't think we want that) or send to db and retrieve it from database in another class.
Here's the proof:
class FirstController extends Controller{
public $variable = "whatever";
/**
* #Route("/page")
*/
public function firstAction(){
$this->variable = "whenever";
return $this->redirectToRoute("path");
}
}
class SecondController{
/**
* #Route("/page/page2", name = "path")
*/
public function secondAction(){
$reference = new FirstController();
$reference->firstAction();
return new Response($reference->variable);
}
}
This code gives an error: Call to a member function get() on null.
When I delete line $reference->firstAction(); there is no error and "whatever" shows up (so the original).

Codeigniter Query result returning Custom result object with setters

How exactly does CI custome object works ?
As per CI documentation You can also pass a string to result() which represents a class to instantiate for each result object (note: this class must be loaded)
$query = $this->db->query("SELECT * FROM users;");
foreach ($query->result('User') as $row)
{
echo $row->name; // call attributes
echo $row->reverse_name(); // or methods defined on the 'User' class
}
}
This is a very nice feature yet what Ci does is it will return an array of User objects and set attributes from row to it.
i have a problem with it that i want to have more control on what attributes to be publicly accessed and what to be modified before setting/getting.
how can i accomplish this ? can i tell CI to pass all attributes to constructor so that class can populate its own data ?
example class User
class User{
private $data=array();
protected $CI;
//public $id,$name,$dob,$gender,$role,$username,$password,$salt,$picture,$lastactive;
function __construct($data=null)
{
$this->data = $data; // i want to save data to a private var and allow attr. throu getters only
}
function set_password($p){
$this->generateSalt();
$this->data->password = $p.$this->data->salt;
}
}
In a nutshell::
I want to use custom_result_object but i dont want codeigniter to populate class attributes for me, instead i want the class to receive those attrs and populate it him self the way he this its appropriate.
I found your question while looking for a solution for myself.
After digging a bit in the documentation I managed to figure it out:
class user_item {
// you can declare all the attributes you want as private
private $id,$name,$dob,$gender,$role,$username,$password,$salt,$picture,$lastactive;
function __construct(){
// you can use the constructor to format data as needed
$this->username = strtouppper($this->username);
}
public function set_password($p){
$this->generateSalt();
$this->password = $p.$this->salt;
}
public function get_password(){
return $this->password;
}
}
Once set up, you can instantiate this class from $this->db->result()
class User_model extends CI_Model {
public function get_user($id){
return $this->db->get_where('users', array('id' => $id), 1)->result('user_item');
}
}
And call any public method or attribute of the class as needed
class Users extends CI_Controller {
function __construct(){
$this->load->model('user');
}
public function profile($user_id){
var $myUser = $this->user->get_user($user_id);
$myUser->set_password('myPassword');
echo $myUser->get_password();
}
}
I have simplified the code to make it clearer, but you get the idea.
this example controller using result array and object
if ($this->session->userdata('id_jurusan') ==1) {
$where=array('id_jurusan'=>$this->session->userdata('id_jurusan'));
$value = $this->session->userdata('id_jurusan');
$value2 = $this->session->userdata('username');
$data['rule']=$this->guru_mod->get_where($where,'forward_changing')->result();
$data['fuzzy']=$this->guru_mod->get_data_all('fuzzy')->result();
$data['artikel']=$this->guru_mod->get_data_all('artikel')->result();
$data['kondisi']=$this->guru_mod->get_where($where,'kondisi')->result();
$data['artikel2'] = $this->guru_mod->get_data_all2('artikel','id_jurusan',$value);
$data['riwayat_rule'] = $this->guru_mod->get_data_all2('forward_changing','username',$value2);
$data['kondisi_rule'] = $this->guru_mod->get_data_all2('kondisi','id_jurusan',$value);
$this->load->view('guru/daftar_rule',$data);
}

Random object per page request with SilverStripe

Lets say you show a random statement per page request and use a function to return a random object like:
Statement::get()->sort("RAND()")->limit("1");
But now in the template you want to reference it twice in different places but it should be the same statement and not a randomly different one. How would you make sure to get the same random object per page request?
What about defining a function with a static variable that remembers the object?
public function rndObj() {
static $obj = null;
if(!isset($obj)){
$obj = Statement::get()->sort("RAND()")->limit("1")->first();
}
return $obj;
}
and then use rndObj in the template.
One way to do this is to fetch the random statement in the controller init function and assign this to a private variable. We add a getRandomStatement function to fetch the random statement:
class Page_Controller extends ContentController {
private $randomStatement;
public function init() {
parent::init();
$this->randomStatement = Statement::get()->sort('RAND()')->limit(1)->first();
}
public function getRandomStatement() {
return $this->randomStatement;
}
}

Using undefined variable or array offset as argument in php

I have a static method that returns or creates an object if it doesn't exist. I'd like to find an elegant way to handle passing an ID argument to the object.
The logic here is that if an ID is passed, the object is created with that ID. If not, it's created with a default ID.
What I had was:
class App {
private $game_obj;
public static function get_game ($arguments) {
if (!isset($this->game_obj))
$this->game_obj = new Game ($arguments[0]);
return $this->game_obj;
}
}
class Game {
private $gameID;
public function __construct ($id=1) {
$this=>gameID = $id;
/* other code */
}
}
When I call App::get_game(5) I get the result I expect. A Game object with a gameID of 5.
When I call App::get_game() I do not get the result I expect, which would be a Game object with a gameID of 1. Instead I get an error about undefined offset in the App::get_game ().
I updated App::get_game() as follows, but it seems particularly inelegant. I'd prefer to define the default ID in the class definition.
class App {
private $game_obj;
public static function get_game ($arguments) {
if (!isset($this->game_obj)) {
$default_id = 1; // I don't like having this here
$id = isset ($arguments[0] ? $arguments[0] : $default_id;
$this->game_obj = new Game ($id);
}
return $this->game_obj;
}
}
Is there a simpler/better/more elegant way to handle this? Ideally one that would keep my default ID declaration in the Game class itself?
did you try
public static function get_game ($arguments=null) {
if($arguments==null) //etc..
?
you can test with :
if(is_object($my_object)) {
}

Accessing parent class objects & variables through $this?

I have one main class "main", which I use to store the configuration and a few objects. However, I am unable to access the objects & variables in a class which extends main, through $this.
Here is some of my main class:
<?php
class main{
// the configuration vars
public $config = array();
// the current user variables
public $uid; // contains the user ID
public $gid; // contains the group ID
// database variables
public $DB; // contains the connection
public $query_id; // contains the query ID
public $query_count; // how many queries have there been?
// cache variables
public $cache;
// start up functions
public function startup(){
// first, set up the caching
if(function_exists('memcache_connect') AND $this->config['use_memcache'] == true){
// start up memcache
require_once('memcache.class.php');
$this->cache = Cache::getInstance();
}
// now, set up the DB
$this->connectToDatabase();
// now, set up the user session
$this->setUserSession();
// are we logged in? If so, update our location
if($this->uid > 0){
// update location
$this->updateUserSession();
}
}
Here is an example of another class
<?php
class user extends main{
public function viewProfile($uid){
exit($this->config['session_prefix']); // displays nothing
if($this->cache->exists('key')){ // fails
The $config value is correctly set up, and I can read it within main but not any sub class.
The $cache->exists object just results in Call to a member function exists() on a non-object.
On index.php, the classes are loaded and set up. The Main class is setup first, and the startup function is called. The configuration array is passed before startup is called.
Can anyone suggest how I can fix this?
Are you sure your array is correctly set up.
This simple test works for me:
<?
class main {
public $test = "Woot";
}
class test extends main {
public function __construct() {
echo $this->test;
}
}
$t = new test();
Just for testing purposes, add a variable $test like in my example to the class main and try to echo it in the user constructor.
I would check if your cache is really set up correctly.
Probably the error is in this if statement:
if(function_exists('memcache_connect') AND $this->config['use_memcache'] == true){
One of these two conditions isn't met, so your $cache variable won't get initialized.
you can use parent:: to referance variables in the parent class.
parent::cache->exists('key')
You can access parent class's attributes by $this->attribute, so the problem should be that you are not calling the constructor of the parent...
Like this code is perfectly working:
<?php
class foo {
public $x="aa";
}
class bar extends foo {
public function barbar()
{
echo $this->x;
}
}
$b = new bar;
$b->barbar();
?>
Though you can also access parent class properties by Parent::attr()

Categories