I have something like this
class Unilevel {
public $contador = 2;
public static function listarLevels($user_id){
if($contador <= 5){
echo '<h1>Nivel '.$contador.'</h1>';
$user = DB::table('matrices')->where('id_user', $user_id)->first();
$actual_user = DB::table('users')->where('id', $user->id_user)->first();
echo $actual_user->username.'<br>';
}
$contador++;
}
}
The function is not working but if i put the variable $contador inside listarLevels works
which is the problem? Thanks
This is how classes work in PHP.
To call $contador within the function when you've declared it as a class property, you use $this->contador
It may be helpful to read the manual on object oriented programming in PHP to get more familiar with it before diving into Laravel. http://php.net/manual/en/language.oop5.php
You can't access your class non static properties in static methods.
Try this.
class Unilevel {
public static $contador = 2;
public static function listarLevels($user_id){
//self::$contador = 4;
}
}
If your method is static you need make your property static too. Their are few other ways too like instantiating the object of the class. Read more about static in PHP.
Related
Why would programmers do some code like this, using the 'this' keyword
private Datalog instance = this;
//constructor
public Datalog() {}
Although you did not mention what language this is, I will assume this is Java.
In Java just as in many other languages a method (function which belongs to an instanciated object) may refer to different fields via different contexts.
meaning that sometimes in order to be able to refer to a field using the this.field will not be good if we want the field of a different context and not the field which belongs to the this (calling object).
Follow demonstration (Note we have here a class within a class - AKA inner class):
public class HumanBody{
HumanBody instance = this;
MainBody mb;
int totalHeight;
public HumanBody() {
mb = new MainBody();
}
public class MainBody {
int totalHeight;
public int getHeightOfMainBodyOnly() {
return instance.totalHeight - totalHeight;
}
}
public int getBodyHeight() {
return totalHeight;
}
}
NOTE: that in getHeightOfMainBodyOnly we do: instance.totalHeight - totalHeight
when instance.totalHeight is the HumanBody totalHeight and just totalHeight is the MainBody total height.
We could not have done it using only this keyword, since this within getHeightOfMainBodyOnly will refer only to the MainBody object caller.
In Javascript this is more common, there a convention is to use:
var self = this;
also for the same reasons I mentioned here!
I have Model_Group that extends ORM.
I have Controller_Group that gets a new ORM:
public function before()
{
global $orm_group;
$orm_group = ORM::factory('Group');
}
...and it has various methods that use it to get different subsets of data, such as...
public function action_get_by_type()
{
global $orm_group;
$type = $this->request->param('type');
$result = $orm_group->where('type', '=', $type)->find_all();
}
Then I have another controller (in a separate module) that I want to use to manipulate the object and call the relevant view. Let's call it Controller_Pages.
$orm_object = // Get the $result from Controller_Group somehow!
$this->template->content = View::factory( 'page1' )
->set('orm_object', $orm_object)
What is the best way to pass the ORM object from Controller_Group to Controller_Pages? Is this a good idea? If not, why not, and what better way is there of doing it?
The reason for separating them out into different controllers is because I want to be able to re-use the methods in Controller_Group from other modules. Each module may want to deal with the object in a different way.
This is the way I would do it, but first I would like to note that you shouldn't use global in this context.
If you want to set your ORM model in the before function, just make a variable in your controller and add it like this.
public function before()
{
$this->orm_group = ORM::factory('type');
}
In your Model your should also add the functions to access data and keep the controllers as small as possible. You ORM model could look something like this.
public class Model_Group extends ORM {
//All your other code
public function get_by_type($type)
{
return $this->where('type', '=', $type)->find_all();
}
}
Than in your controllers you can do something like this.
public function action_index()
{
$type = $this->request->param('type');
$result = $this->orm_group->get_by_type($type);
}
I hope this helps.
I always create an helper class for stuff like this
Class Grouphelper{
public static function getGroupByType($type){
return ORM::factory('Group')->where('type','=',$type)->find_all();
}
}
Now you're been able to get the groups by type where you want:
Grouphelper::getGroupByType($type);
I'm not sure what's happening here, perhaps someone has a clue:
On my layout I have this:
<div id="main-content" class="<?= $this->getMainClass(); ?>">
On components>controller class I have this:
private $_mainCssClass;
public function setMainCssClass($className) {
if (is_string($className)) {
$this->_mainCssClass = $className;
} else {
quickDump($className);
}
}
public function getMainCssClass() {
return $this->_mainClass;
}
What I'm not getting is, despite the fact that we are using accessors, and the mainCssClass property has an underscore and it's declared private, I can access it trough:
$this->MainCssClass;
The same as: $this->getMainCssClass()
Why is that ?
You don't access the private _mainCssClass but rather getMainCssClass() which is a public method. So you are allowed to call this method from outside and you can also access mainCssClass (which is just a shorthand for getMainCssClass()).
And from getMainCssClass() you are allowed to access any private variable inside the same class. There's no restriction that you could not return the value of a private variable.
It's just a shorter syntax, more convenient to read and write.
See the Yii Framework Guide for details and the source code for some impressions about the "magic".
Please see the code below :
package bk;
public class A {
protected void methodA() {
System.out.println("Calling the method A !");
}
}
// And I have an another package :
package com;
import bk.A;
public class B extends A {
public void methodB() {
System.out.println("Goi phuong thuc B !");
}
public static void main(String[] args) {
A a = new B();
a.methodA();
}
}
How can I allow a to call methodA()?
Cause methodA() is protected and it can be called within derived classes only. Change it to public if you want to call it like this
Protected methods can only be called from within the class itself, or from derived classes.
The a variable is declared as a variable of type A. Class A itself has no publicly available methodA, so you cannot call it.
Yes, you assign a new B instance to the a variable and the a.methodA() statement is inside the derived B class, but the compiler only sees that a is of type A. It could be any other subclass of A as well, in which case you still wouldn't have access to methodA.
You'll have to tell the compiler that the a variable is actually of type B. Then you will be able to call methodA, because you're calling it from within class B.
B a = new B();
You are trying to access methodA() like it is public. Declaring simply methodA() in the B class is fine, but you cannot do a.methodA().
Conversely if it wasn't a method and simply protected int a;
you could do
a = 1; in class B
but
A a = new A();
a.a = 1;
is not legal
A protected method is visible to inheriting classes, even not part of the same package. A package scope (default) method is not. That is the only difference between protected and package scope.
The theory is that someone extending your class with protected access knows more about what they are doing than someone who is merely using it with public access. They also need more access to your class’s inner workings. Other than that, protected behaves like default package access.
I am using JNA to access a custom DLL which seems to be using the FAR PASCAL Calling Conventions, but the JVM crashes every time i try to access it.
Development Guide of the dll says:
BOOL FAR PASCAL GetIomemVersion(LPSTR);
And Dependency Walker tells me:
_GetIomemVersion#4
public class PebblePrinter {
public interface Iomem extends StdCallLibrary {
boolean _GetIomemVersion(String version);
}
String version;
Iomem INSTANCE;
StdCallFunctionMapper myMapper;
public PebblePrinter() {
HashMap optionMap = new HashMap();
myMapper = new StdCallFunctionMapper();
optionMap.put(Library.OPTION_FUNCTION_MAPPER, myMapper);
INSTANCE = (Iomem)Native.loadLibrary("iomem", Iomem.class,optionMap);
}
public String getIomemVersion(){
INSTANCE._GetIomemVersion(version);
return version;
}
}
With C# code it works well using
[DllImport("iomem.dll", EntryPoint = "_GetIomemVersion#4")]
public static extern bool GetIomemVersion(IntPtr version);
Can you tell me what i am doing wrong?
Thanks in advance!!!
Problem solved,
I've just used the wrong parameter ..
GetIomemVersion needs a Pointer
boolean _GetIomemVersion(Pointer version);
public String getIomemVersion(){
Memory m = new Memory(1024);
Pointer x = m.getPointer(0);
INSTANCE._GetIomemVersion(x);
return x.getString(0);
}