php OOP PDO Database connection and processes

as I am learning to code PHP with PDO and OOP, I’ve come across to this connection code. I would like to ask anybody if this is okay. I’ve tried to use this with my code and it did work fine. I was able to Login and Generated record. But does this code enough? <?php class db { private $conn;…
Read more

How to change object’s properties’ places?

I have an object playerInfoObject with properties w1 and w2, which are also objects. I need to change their places between each other They become equal when I’m trying to do this: var tmp = playerInfoObject.w1; playerInfoObject.w1 = playerInfoObject.w2; playerInfoObject.w2 = tmp; How can I solve this problem? Thank you in advance Source: oop

Why is my code not working when t is similar to MDN? (OOP)

This is MDN’s ball velocity code: var ball = { x: 100, y: 100, vx: 5, vy: 2, radius: 25, color: ‘blue’, draw: function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, true); ctx.closePath(); ctx.fillStyle = this.color; ctx.fill(); } }; function draw() { ctx.clearRect(0,0, canvas.width, canvas.height); ball.draw(); ball.x += ball.vx; ball.y += ball.vy; raf = window.requestAnimationFrame(draw); }; canvas.addEventListener(‘mouseover’, function(e){ raf =…
Read more

Polymorphism on simple example

i think i’m starting to understand this topic, but not completely. can smbd explain to me on this example. public class Solution { public static void main(String[] args) { Cow cow = new Whale(); System.out.println(cow.getName()); } public static class Cow { public String getName() { return “Im cow”; } } public static class Whale extends Cow { public String getName()…
Read more

Multiple virtual inheritance

I found the below code in our code base at work, and I think it looks strange. The class RetainCounting is used for implementing reference counting. 1.Should the IProvider really derive from RetainCounting? Isn´t it better if it was just an abstract base class and then let the class that implements IProvider i.e. ProviderImpl derive from the “RetainCounting” instead (i.e….
Read more

Single Responsibility Principle the correct approach

SRP really bug me. I know how to find responsibility what I dont know is how to assemble them correctly e.g class Modem{ public void dial(){//code here} public void hangup(){//code here} public void send(){//code here} public void recive(){//code here} } In this common example we have connection and communication responsibility. So we divide that into 2 new interfaces interface Connection{…
Read more

How to access info from an instance from another instance in c++?

I am just started learning OOP concepts and to help myself learning, I have created a Characters class. From this class I have made instance called main and an instance called monster. Here is the code for the class: #include <iostream> #include <string> using namespace std; class Character { public: string name; float health; int attackLevel; int defenseLevel; void setAttr(string…
Read more

Callback and tight coupling

I’m refactoring some parts of my code, one of them has callback function. I’m not really sure of the relevance of callback there, because it introduces tight-coupling. Let me give you an example: public class SimpleExample { OtherClass oc; Callback callback; … public void register(Callback c) { this.callback = c; } public void doStuff() { callback.push(oc); } … } The…
Read more

Calling a public function without creating a new object JS

I’m playing with JS “oop” (or should i say “pop” – prototype oriented programming?) and tried to do something like static method and functions. I can call function from prototype object but that isn’t exactly static function, is it? I have that code: function a(val){ this.val = val; console.log(this); this.foo = function(){ console.log(‘hi’); } } And i want to call…
Read more